我尝试使用下面的代码解析字符串数组,但是从未打印过所需的数据!任何人都可以告诉我如何解决它?谢谢
$ data数组结构:
Array
(
[js] => Array
(
[total_items] => 20
[max_page_items] => 2
[selected_item] => 0
[cur_page] => 0
[data] => Array
(
[0] => Array
(
[tmp] => 1
[name] => mango
[abc] => abcd4 http://mysite/items/1234
[number] => 1123
[itemCategory_title] => fruits
[logo] => 2123.png
[itemCategory_id] => 90
)
[1] => Array
(
[tmp] => 0
[name] => cherry
[abc] => abcd4 http://mysite/items/1235
[number] => 1124
[itemCategory_title] => fruits
[logo] => 2124.png
[itemCategory_id] =>
)
)
)
[text] => no error
)
php代码:
<?
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2);
foreach( $data as $item ) {
echo $item['tmp'];
echo $item['name'];
echo $item['abc'];
echo $item['number'];
echo $item['itemCategory_title'];
echo $item['log'];
echo $item['itemCategory_id'];
}
?>
答案 0 :(得分:1)
应该是:
foreach ($data['js']['data'] AS $item)
因为数组在$data
中嵌套了几个级别。
请注意,您需要调用json_decode($code2, true)
来获取类似的关联数组。默认情况下,它返回一个对象,而不是一个数组,所以你可以这样做:
foreach ($data->js->data as $item) {
echo $item->tmp;
echo $item->name;
...
}