0

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.

5
  • The associative array in question is not valid. 'sport' => 'Football' you are missing a comma. Is that a typo? Commented Nov 2, 2017 at 18:36
  • Object.values or Object.entries. May want Babel or a polyfill for older browsers. Commented Nov 2, 2017 at 18:40
  • Fixed, it was a transcription error Commented Nov 2, 2017 at 18:40
  • What is xxx in xxx.map? Commented Nov 2, 2017 at 18:45
  • xxx is an example, depending on the zone of the array I get the odds.map error or matchs.map error... Commented Nov 2, 2017 at 18:56

1 Answer 1

0

Looking at your associative array structure, I realise that the JSON form of $matches will look something like,

{"10689":{"id":"10689","sport":"Football", ...

and when you decode this JSON in javascript, it becomes an object and object.map is not a valid method call (as .map is only applicable for arrays and not objects).

you will probably have to use Object.keys(<your_json>).map.

If you don't need keys, you can use Object.values(<your_json>).map

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.