我尝试搜索但找不到。抱歉,如果解决方案已经存在,则错过了...
代码
$json = '[{"id":"2","value":"1"},{"id":"1","value":"1"},{"id":"3", "value":""},{"id":"4","value":""},{"id":"5","value":""},{"id":"6" ,"值":""},{"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;
}
}
输出
在数组键 9 处找到的值
但这仅在我用单引号中的值静态指定 $json 时才有效......但在我的 joomla 项目中,值是在类变量的帮助下获取的,所以当我使用$json=$item->extra_fields
而不是给它一个静态字符串时,$json
它不起作用。 ......
代码
$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;
}
}
输出
警告:在第 484 行的 /components/com_k2/views/item/view.html.php 中为 foreach() 提供的参数无效
更新:
echo "JSON: $json<br/><br/>";
echo "DUMP: ".var_dump($myArray);
输出
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:
--> 对 json_decode 使用 true 不会改变输出
Stripslashes 也没有工作。检查 $item->extra_fields 是字符串类型