从数组PHP获取数据 [英] Get Data from Array PHP
问题描述
我无法从数组中获取数据,当我运行我的foreach循环时,它只显示了3个列表项的空白列表。
当我打印数组时,它看起来像这样 - >
数组
$ p $
(
[1] =>数组
(
[id] => 10
[orderinfo] => Array
[userid ] => 210
[date] => 2013-06-20 13:46:27
)
[2] =>数组
[id] => 18
[orderinfo] => helo
[userid] => 210
[date] => 2013-06-20 15: 04:58
)
[3] =>数组
(
[id] => 19
[orderinfo] => { order:[{id:2,name:Basil Cress,qty:1},{id:4,name:Sakura Mix, qty:1},{id:6,name:Beetroot Shoots,qty:2},{id:28,name ,qty:2},{id:24,name:Orange Capsicums,qty:1}]}
[userid] => 210
[date] => 2013-06-20 15:06:46
)
)
我的代码到目前为止。 ){
$ orderinfo = json_decode($ item-> orderinfo,true);
$ orderitem [] = array(
'date'=> $ item-> date,
'productname'=> $ orderinfo ['name'],
'productqty'=> $ orderinfo ['qty'],
);
}
echo< pre>;
print_r($ orderdata);
echo< / pre>;
?>
<?php foreach($ orderitem as $ orderitems){?>
< li>
<?php echo $ orderitems ['date']; ?>
< / li>
<?php}; ?>
尝试在循环之前声明你的数组。你已经这样做了吗?
$ orderitem = array();
foreach($ orderdata as $ item){
$ orderinfo = json_decode($ item-> orderinfo,true);
$ orderitem [] = array(
'date'=> $ item-> date,
'productname'=> $ orderinfo ['name'],
'productqty'=> $ orderinfo ['qty'],
);
$ b 你也可以尝试填充你的数组,像这样:
array_push($ orderitem,array($ b $'date'=> $ item-> date,
' productname'=> $ orderinfo ['name'],
'productqty'=> $ orderinfo ['qty'],
));
I cant quite get my data from an array, it just shows a blank list of 3 list items when I run my foreach loop.
When I print my array it looks like this ->
Array
(
[1] => Array
(
[id] => 10
[orderinfo] => Array
[userid] => 210
[date] => 2013-06-20 13:46:27
)
[2] => Array
(
[id] => 18
[orderinfo] => helo
[userid] => 210
[date] => 2013-06-20 15:04:58
)
[3] => Array
(
[id] => 19
[orderinfo] => {"order":[{"id":"2","name":"Basil Cress","qty":"1"},{"id":"4","name":"Sakura Mix","qty":"1"},{"id":"6","name":"Beetroot Shoots","qty":2},{"id":"28","name":"Celery","qty":2},{"id":"24","name":"Orange Capsicums","qty":"1"}]}
[userid] => 210
[date] => 2013-06-20 15:06:46
)
)
My code so far..
foreach ($orderdata as $item) {
$orderinfo = json_decode($item->orderinfo, true);
$orderitem[] = array(
'date' => $item->date,
'productname' => $orderinfo['name'],
'productqty' => $orderinfo['qty'],
);
}
echo "<pre>";
print_r($orderdata);
echo "</pre>";
?>
<?php foreach ($orderitem as $orderitems) { ?>
<li>
<?php echo $orderitems['date']; ?>
</li>
<?php }; ?>
解决方案 Try to declare your array before the loop like this. Are you already doing this?
$orderitem = array();
foreach ($orderdata as $item) {
$orderinfo = json_decode($item->orderinfo, true);
$orderitem[] = array(
'date' => $item->date,
'productname' => $orderinfo['name'],
'productqty' => $orderinfo['qty'],
);
}
You can also try to populate your array differently, like this:
array_push($orderitem,array(
'date' => $item->date,
'productname' => $orderinfo['name'],
'productqty' => $orderinfo['qty'],
));
这篇关于从数组PHP获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!