1

I have seen many posts on here about converting a multi dimensional array into a string but not the other way around so I have a question to ask. I have got the following string of data which is retrieved from a JQuery array via a post:

["[email protected], [email protected]","http://www.gardengamesltd.co.uk/acatalog/contactus.html"],["[email protected]","http://www.gardengames.com/contact/"],["[email protected]","http://www.gardengamesandleisure.com/ContactUs.aspx"],["[email protected]","http://www.kentgardengameshire.com/contact-us.html"],["[email protected]","http://www.gardengamesuk.com/contact.php"],["[email protected]","http://www.gardenknightgames.com/contact/"],["[email protected]","http://www.just-garden-games.co.uk/"]

What I am wanting to do is convert it into an array which looks like so:

Array
(
    [0] => Array
        (
            [Email] => [email protected], [email protected]
            [FB] => http://www.gardengamesltd.co.uk/acatalog/contactus.html
        )

    [1] => Array
        (
            [Email] => [email protected]
            [FB] => http://www.gardengames.com/contact/
        )
    [2] => Array
        (
            [Email] => [email protected]
            [FB] => http://www.aaeventhire.com/pricing/garden-games
        )

)

I realize I could use $array = explode('","', $harvest_data); however this is only going to give me a single level array and ideally I am wanting to keep email, fb inside an inner array.

Has anyone got any ideas on how I can go about doing this?

Thanks.

5
  • You'll have to explode as you have, then loop that and explode the contents of that array, and combine it back. Commented Oct 30, 2013 at 16:50
  • I would just JSON encode the array, post it to PHP and then JSON decode it. Commented Oct 30, 2013 at 16:51
  • So I would explode by ],[ first then "," second inside a foreach loop of the first exploded array? I was looking into this but I am not sure how to make an array once I have looped into the initial array Commented Oct 30, 2013 at 16:52
  • David your reply looks good, I'm just going to look around for an example of a JSON encode and decode Commented Oct 30, 2013 at 16:53
  • Javascript side: developer.mozilla.org/en-US/docs/JSON PHP Side: php.net/manual/en/function.json-decode.php Commented Oct 30, 2013 at 16:55

1 Answer 1

2

As it is, your string is not valid JSON. Wrapping it in a pair of []'s would work in this case so if the input always has this form, this would work:

$json_string = '[' . $your_string . ']';
$your_array = json_decode($json_string);

However, it would be best to make sure that your front-end / javascript posts valid JSON to begin with.

Working example.

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

1 Comment

The javascript was in fact posting valid JSON I simply stripped the outside [] while trying to explode, thanks for the help.

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.