E
E
Evgeny Ivanov2018-02-12 12:59:14
PHP
Evgeny Ivanov, 2018-02-12 12:59:14

How to get "randomly generated" value of json array?

There is an array like

{"success":1,"return":
{
"236015091":{"status":0,"pair":"new","type":"one","amount":0.05,"rate":188.0},
"244806736":{"status":1,"pair":"old","type":"two","amount":0.3,"rate":25.0},
"244901138":{"status":0,"pair":"sold","type":"three","amount":30.5,"rate":0.7}
},
"stat":{"isSuccess":true,"serverTime":"00:00:00.0000564","time":"00:00:00.0426275","errors":null}}

How in that case to receive for each "array"
Its name - for example 236015091?
Its element value is eg 236015091->pair (new)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Boris Korobkov, 2018-02-12
@BorisKorobkov

json_decode + foreach

A
Alexander, 2018-02-12
@AK-VoronM

Try like this:

$arr = json_decode($json, true);
foreach ($arr['result'] as $k => $in_arr) {
    echo "Имя внутреннего элемента: {$k}<br>";
    echo "Перебор вложенных элементов:<br>";
    foreach ($in_arr as $in_k => $in_v) {
        echo "{$in_k} => {$in_v} ";
    }
    echo "<br>";
    echo "Только определенный элемент внутреннего массива:<br>";
    echo $in_arr['pair'], "<br>";
}

If only names are needed:
$arr = json_decode($json, true);
echo "<pre>";
var_dump(array_keys($arr['result']));
echo "</pre>";

The actual solution depends on the problem.

E
Evgeny Ivanov, 2018-02-12
@logpol32

$obj = json_decode($data,true);
foreach($obj as $obj_array)
{
echo $obj_array;
}

1,Array,Array
There are 2 problems.
1 Arrays of different levels of nesting.
2 unknown array names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question