G
G
GRO242020-10-29 06:38:06
PHP
GRO24, 2020-10-29 06:38:06

How to find object without JSON decode PHP key?

Welcome all.
Get JSON file, decode it

{
    "response": [{
        "id": "29654",
        "user": "USER",

    }, 
......
]}

$array_decode = json_decode($url,true);
I want to call directly -> $array_decode['response']['user'];
But without a key it does not work, why?
We only work like this
$array_decode['response'][идентф_ключа]['user'];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2020-10-29
@alexalexes

Rebuild the users array:

$out_users = [];
foreach($array_decode['response'] as $user)
  $out_users[$user['id']]['user'] = $user['user'];

Then you can access as you like:
$out_users[key_id]['user'];
PS: Rebuild option if you have a lot of users and you are limited by script RAM:
$out_users = [];
foreach($array_decode['response'] as &$user) // обратите внимание на доступ по ссылке & к элементу массива
{
  $out_users[$user['id']]['user'] = $user['user'];
  unset($user); //удаляем элемент из массива $array_decode['response'], чтобы не росло использование памяти.
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question