C
C
CrewCut2016-02-08 12:50:16
PHP
CrewCut, 2016-02-08 12:50:16

Where does the extra nesting come from when decoding JSON into a PHP array?

There is JSON:

$cats_price = '[{"kurtki-i-palto-zimnie-muzhskie":40},{"dublenki-iz-naturalnoj-ovchiny-muzhskie":40}]';

Through json_decode($cats_price, true);I distill into an array, the output is:
array(2) {
  [0]=>
  array(1) {
    ["kurtki-i-palto-zimnie-muzhskie"]=>
    int(40)
  }
  [1]=>
  array(1) {
    ["dublenki-iz-naturalnoj-ovchiny-muzhskie"]=>
    int(40)
  }
}

Why are there so many levels in the array? I'm hoping for this result:
Array(
"kurtki-i-palto-zimnie-muzhskie"=> 40,
"dublenki-iz-naturalnoj-ovchiny-muzhskie"=> 40
)

How to achieve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2016-02-08
@CrewCut

Your json should then look like this:

$cats_price = '{
    "kurtki-i-palto-zimnie-muzhskie":40, 
    "dublenki-iz-naturalnoj-ovchiny-muzhskie":40
}';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question