K
K
KRHD2015-12-10 21:13:09
PHP
KRHD, 2015-12-10 21:13:09

How to get json in foreach?

Here is the answer I received, how to display it in foreach?

{"historyList":[{"order_id":316052,"price":55,"previ_id":"109618","title":"\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u0430\u0434\u043c\u0438\u043d","date":"1434017352"},{"order_id":315443,"price":75,"previ_id":"109619","title":"\u0421\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c","date":"1433997900"},{"order_id":306896,"price":300,"previ_id":"109625","title":"\u0412\u043b\u0430\u0434\u0435\u043b\u0435\u0446","date":"1433693384"},{"order_id":302606,"price":75,"previ_id":"109619","title":"\u0421\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c","date":"1433530301"},{"order_id":302553,"price":25,"previ_id":"109610","title":"\u041a\u0440\u0435\u0430\u0442\u0438\u0432","date":"1433528927"}]}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Immortal_pony, 2015-12-10
@Immortal_pony

Use the json_decode function .
More or less like this:

$json_string = '{"historyList":[{"order_id":316052,"price":55,"previ_id":"109618","title":"\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u0430\u0434\u043c\u0438\u043d","date":"1434017352"},{"order_id":315443,"price":75,"previ_id":"109619","title":"\u0421\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c","date":"1433997900"},{"order_id":306896,"price":300,"previ_id":"109625","title":"\u0412\u043b\u0430\u0434\u0435\u043b\u0435\u0446","date":"1433693384"},{"order_id":302606,"price":75,"previ_id":"109619","title":"\u0421\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c","date":"1433530301"},{"order_id":302553,"price":25,"previ_id":"109610","title":"\u041a\u0440\u0435\u0430\u0442\u0438\u0432","date":"1433528927"}]}';
$some_data = json_decode($json_string);

if (!json_last_error() && !empty($some_data->historyList)) {
    foreach ($some_data->historyList as $order) {
        // Do something with $order
    }
}

I
Ivanq, 2015-12-10
@Ivanq

Everyone here is wrong. Code - json_decode("ваша строка", true);. true means an array is returned, not an object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question