I have a PHP multidimensional associative array like this:
$matchs = array(
'10689' => array(
'id' => '10689',
'sport' => 'Football',
'player_1' => array(
'id' => '22',
'name' => 'Mike Oldfield',
'odds' => array(
'bookie_1' => '1.20',
'bookie_2' => '1.21',
'bookie_3' => '1.22'
)
),
'player_2' => array(
'id' => '122',
'name' => 'Fran Sinatra',
'odds' => array(
'bookie_1' => '4.10',
'bookie_2' => '4.11',
'bookie_3' => '4.22'
)
)
),
'10829' => array(
'id' => '10829',
'sport' => 'Basketball',
'player_1' => array(
'id' => '522',
'name' => 'Frank Black',
'odds' => array(
'bookie_1' => '2.01',
'bookie_2' => '2.02',
'bookie_3' => '2.04'
)
),
'player_2' => array(
'id' => '122',
'name' => 'Freddie Mercury',
'odds' => array(
'bookie_1' => '1.87',
'bookie_2' => '1.86',
'bookie_3' => '1.85'
)
)
),
);
I need to pass that array to React App. Then I make something like this:
<?php echo json_encode($matchs, JSON_FORCE_OBJECT); ?>
Then with axios (I have also tried with jquery and other libraries) I get the json file. Everything perfect for now.
The problem comes when I try to traverse the array. I get an error 'TypeError: xxx.map is not a function'. Thanks to this thread what has been achieved in part: React: Map multidimensional array with different keys
The question is how could I remove the keys of this array to traverse without using Object.keys
I have tried echo json_encode(array_values($matchs, JSON_FORCE_OBJECT)) , the problem is that it only eliminates the keys of the first level. In my array example 'player_1', 'player_2' and 'odds' remains.
'sport' => 'Football'you are missing acomma. Is that a typo?Object.valuesorObject.entries. May want Babel or a polyfill for older browsers.xxxinxxx.map?xxxis an example, depending on the zone of the array I get theodds.map errorormatchs.map error...