JSON to PHP Associative array

前端 未结 3 979
夕颜
夕颜 2021-01-01 06:11

would any of you know a good way to put this into an associative array . I have tried json_decode but found it to not be much help.

This is the data i need to put in

3条回答
  •  醉话见心
    2021-01-01 06:43

    json_decode works for me on your data:

    print_r(json_decode('{
           "data": [
              {
                 "name": "Joe Bloggs",
                 "id": "203403465"
              },
              {
                 "name": "Fred Bloggs",
                 "id": "254706567"
              },
              {
                 "name": "Barny Rubble",
                 "id": "453363843"
              },
              {
                 "name": "Homer Simpson",
                 "id": "263508546"
              }
           ]
        }
    ', true));
    

    Output:

    Array
    (
        [data] => Array
            (
                [0] => Array
                    (
                        [name] => Joe Bloggs
                        [id] => 203403465
                    )
    
                [1] => Array
                    (
                        [name] => Fred Bloggs
                        [id] => 254706567
                    )
    
                [2] => Array
                    (
                        [name] => Barny Rubble
                        [id] => 453363843
                    )
    
                [3] => Array
                    (
                        [name] => Homer Simpson
                        [id] => 263508546
                    )
    
            )
    
    )
    

    Setting the second argument to true returns an associative array.

提交回复
热议问题