B
B
bone_games2021-01-15 13:01:31
PHP
bone_games, 2021-01-15 13:01:31

How to change json array?

Good day to all, there is a json array of type

"group": [
                {
 "id": "12",
"value": "3"
},
       {
"id": "15",
"value": "1"
}
 ]


How can I get the array to become like this (PHP)
"group": 
            {
            "12": "3",
             "15": "1"
            }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mors Clamor, 2021-01-15
@66demon666

Parse -> modify -> assemble

S
Slava Rozhnev, 2021-01-15
@rozhnev

Don't thank:

$data = json_decode($json, true);

$data['group'] = array_reduce(
  $data['group'],
  function($res, $i) {
    $res[$i['id']] = $i['value'];
    return $res;
  },
  []
);

PHP online
Result:{"group":{"12":"3","15":"1"}}

A
Anton Shamanov, 2021-01-15
@SilenceOfWinter

json_decode/array_column/json_encode

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question