json_decode problem with joomla
I tried searching but could not find. Sorry If have missed if the solution is already present...
Code
$json = '[{"id":"2","value":开发者_运维知识库"1"},{"id":"1","value":"1"},{"id":"3","value":""},{"id":"4","value":""},{"id":"5","value":""},{"id":"6","value":""},{"id":"7","value":""},{"id":"8","value":""},{"id":"9","value":""},{"id":"10","value":"1"}]';
$myArray = json_decode($json);
foreach ($myArray as $key => $v) {
if ($v->id == 10 && ($v->value == 0 || $v->value == 1)) {
echo 'Value found at array key ' . $key;
}
}
OUPUT
Value found at array key 9
But this works only when i statically specify the $json with value in single quotes...But in my joomla project the values is fetched with help of class variable
so when i when i use $json=$item->extra_fields
instead of giving a static string to $json
it doesnt work......
code
$json=$item->extra_fields;
$myArray = json_decode($json);
foreach ($myArray as $key => $v) {
if ($v->id == 10 && ($v->value == 0 || $v->value == 1)) {
echo 'Value found at array key ' . $key;
}
}
OUTPUT
Warning: Invalid argument supplied for foreach() in /components/com_k2/views/item/view.html.php on line 484
UPDATE:
echo "JSON: $json<br/><br/>";
echo "DUMP: ".var_dump($myArray);
OUTPUT
JSON: [{"id":"2","value":"1"},{"id":"1","value":"1"},{"id":"3","value":""},
{"id":"4","value":""},{"id":"5","value":""},{"id":"6","value":"<br \/>"},
{"id":"7","value":"<br \/>"},{"id":"8","value":"<br \/>"},{"id":"9","value":"<br \/>"},
{"id":"10","value":"1"}]
NULL DUMP:
--> Using true with json_decode doesnt alter the output
Stripslashes also didnt worked. Checked $item->extra_fields is a string type
This has nothing to do with your code, nor with the JSON data you're trying to decode, but is a PHP configuration error (or done on purpose). I've come across this multiple times, but simply put the function has been disabled. Note that the function is not working rather than actually disabled, but the result is the same, an empty return value.
Best solution is to use an alternative code (method) to decode your JSON data, which can be found on the PHP website:
function json_decode($json)
{
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++)
{
if (!$comment)
{
if (($json[$i] == '{') || ($json[$i] == '[')) $out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']')) $out .= ')';
else if ($json[$i] == ':') $out .= '=>';
else $out .= $json[$i];
}
else $out .= $json[$i];
if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
}
eval($out . ';');
return $x;
}
This code is not pretty, but it does the trick. I've used this code every now and then to decode JSON data on servers that have similar problems you describe and I've yet to come across data you can't decode using this function.
@KilZone: Thanks for the reply. I haven't tried your code, but the string that was being fetched from the database was showing properly in the brower, but when i checked it by viewing the source, the string was having "
instead of double quote.
So I just used the below code to replace, and it solved my problem.
json_decode(str_replace(""","\"",$item->extra_fields))
Thank you everyone for the reply.
精彩评论