views:

172

answers:

2

Hey guys, 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 and:

preg_match_all('|"name": "(.*?)",|',$json,$matches);

Seems to match it (in expresso) but returns an empty array in php.

This is the data i need to put into an associative array:

   {
       "data": [
          {
             "name": "Joe Bloggs",
             "id": "203403465"
          },
          {
             "name": "Fred Bloggs",
             "id": "254706567"
          },
          {
             "name": "Barny Rubble",
             "id": "453363843"
          },
          {
             "name": "Homer Simpson",
             "id": "263508546"
          }
       ]
    }

EDIT:

After the answer was returned and I accepted the answer, I remembered why I thought that the json_decode wasn't much help:

Instead of having an associative array like so:

[0] => Array
(
    [name] => Joe Bloggs
    [id] => 203403465
)

I wanted

Array
(
    [Joe Bloggs] => 45203340465
    [Fred Bloggs] => 65034033446
)

Unfortunately, I had forgotten this at the time.. (I know, that was kinda daft of me seeing as it was my problem, and I asked the question) but I have resolved my issue now anyway.

Thanks for all of your help!

+1  A: 

i asume your json comes via ajax.... (otherwise the code works with json_decode) so be sure the js json stringifys your object and

you'll need to stripslashes before json_decode ;-) in php

helle
Ah ok, thanks, ill give it a go :o)
Chief17
It doesn't come via AJAX but for some reason, now when I use json_decode it works as i initally expected it to. Very strange because when I remove stripslashes, it also works... maybe I messed something up when I previously tried it. Many Thanks helle for nudging me back on track :o)
Chief17
hmm ... okay if it works right now - cool :-)
helle
+3  A: 

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.

webbiedave
Thanks for your answer webbiedave, if you view my last comment on helle's comment you'll see what I did. +1 for the response I was looking for!
Chief17