简体   繁体   English

从Array PHP获取数据

[英]Get Data from Array PHP

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. 我无法从数组中获取数据,只是在运行foreach循环时显示了3个列表项的空白列表。

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'],
        ));

Look at the structure of the the JSON for $orderInfo . 查看$orderInfo的JSON结构。 It is a nested array. 它是一个嵌套数组。 So $orderInfo['name'] doesn't exist. 所以$orderInfo['name']不存在。 You want $orderInfo[0]['name'] or some other numerical index to fill in the data. 您需要$orderInfo[0]['name']或其他一些数字索引来填充数据。

This is an array of objects which gets decoded to an array of associative arrays. 这是一个对象数组,它被解码为关联数组数组。 You need to travel one more level down to get the name. 您需要再往前走一层以获得名称。

[
    {"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"}
]

Could it be just your css?? 它可能只是你的CSS? Background color with the same text color? 背景颜色与文字颜色相同? I know it's silly but who knows... 我知道这很傻但谁知道......

Try a print_r($orderitem); 试试print_r($ orderitem); just like you did with $orderdata 就像你使用$ orderdata一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM