4

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 }; ?>
4
  • 2
    can you post your json? Commented Jun 20, 2013 at 18:03
  • Is orderinfo consistent for each item, or is it sometimes an array and sometimes a JSON string? Commented Jun 20, 2013 at 18:04
  • The json is inside the array, but It still doesn't explain why the date does not appear? Commented Jun 20, 2013 at 18:05
  • $item is an array: $item['orderinfo'] Commented Jan 16, 2018 at 16:51

3 Answers 3

2

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'],
        ));
Sign up to request clarification or add additional context in comments.

Comments

1

Look at the structure of the the JSON for $orderInfo. It is a nested array. So $orderInfo['name'] doesn't exist. You want $orderInfo[0]['name'] or some other numerical index to fill in the data.

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

1 Comment

Ok that makes sense, but it should still show the date in the list item it currently just shows blank.
1

Could it be just your css?? Background color with the same text color? I know it's silly but who knows...

Try a print_r($orderitem); just like you did with $orderdata

1 Comment

Yeah tried to take a look and nothing just a list of bullet points

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.