2

So i have a JSON like this

    {
        orderID: "8",
            .....
        OrderLines: {
            OrderLine: [
                {
                    orderLineID: "42",
                    quantity: "25",
                    price: "0.69",
                    originalPrice: "0.69",
                    checkedIn: "2",
                    numReceived: "0",
                    timeStamp: "2015-01-03T19:37:38+00:00",
                    orderID: "8",
                    itemID: "43944"
                },
                {
                    orderLineID: "44",
                    quantity: "75",
                    price: "0.69",
                    originalPrice: "0.69",
                    checkedIn: "25",
                    numReceived: "0",
                    timeStamp: "2015-01-07T20:07:29+00:00",
                    orderID: "8",
                    itemID: "43948"
                },
               ...
            ]
        }
    }

But when it's only one orderLine is looks like this

    {
        orderID: "20",
           ....
        OrderLines: {
            OrderLine: {
                orderLineID: "118",
                    quantity: "24",
                    price: "9",
                    originalPrice: "9",
                    checkedIn: "44",
                    numReceived: "0",
                    timeStamp: "2015-01-20T19:22:54+00:00",
                    orderID: "20",
                    itemID: "37826"
            }
        }
    }

When i do foreach in first case it gives me correct results (i'm getting needed data from each of orderlines), but when it's only 1 item in the order it goes through the array of that 1 orderline and gives me wrong data.

foreach ($orderLines['OrderLine'] as $orderLine) {
               echo $orderLine['quantity'];
               echo $orderLine['numReceived'];
}

How should i let it know that when it's only 1 order to give me just the right rows from this array ?

6
  • 1
    Where does the JSON come from? Can you edit that? I would think you'd rather keep the structure the same in the first place. Commented Jan 27, 2015 at 20:48
  • what is wrong data and right rows? Commented Jan 27, 2015 at 20:50
  • Have you looked at json_decode() : php.net/manual/en/function.json-decode.php Commented Jan 27, 2015 at 20:51
  • decode to native php array, then check if $decoded['OrderLines']['OrderLine'] is an array or a stdclass object? Commented Jan 27, 2015 at 20:51
  • use php json_decode and json_encode functions Commented Jan 27, 2015 at 20:51

1 Answer 1

1

Assuming you're already using json_decode() to convert this JSON into a PHP array/object, you could check to see if OrderLine is an array, and only do the loop if that's the case:

if (is_array($orderLines['OrderLine']) {
    foreach ($orderLines['OrderLine'] as $orderLine) {
        echo $orderLine->quantity;
        echo $orderLine->numReceived;
    }
} else {
    echo $orderLines['OrderLine']->quantity;
    echo $orderLines['OrderLine']->numReceived;
}
Sign up to request clarification or add additional context in comments.

4 Comments

It still returns true, since it is an array. In first case - it is an array of orderline arrays , in second - it's just an array of one orderline
@user3862703 Are you using json_decode()? If so, things enclosed by [ ] should be arrays and things enclosed by { } should be stdClass objects.
or cast it to an array to avoid the is_array method.
@Ortix92 then you would just have to use a different method to determine if there was just one OrderLine or more.

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.