Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON string to PHP Array

Tags:

json

php

I have the following JSON string, which was an Objective C array then encoded to JSON:

 $jsonString=[\"[email protected]\",\"[email protected]\",\"[email protected]\"]

I want to convert this to a regular PHP array. I've tried many things but none of them seem to work:

$arrayOfEmails=json_decode($jsonString); //doesn't work
$arrayOfEmails=(array)json_decode($jsonString); //doesn't work

Any ideas?

Edit:

I'm still not getting it to work.

$decodeEmails=$_POST["arrayOfEmails"];
sendResponse(200, $decodeEmails);
//the above returns exactly whats after this colon:[\"[email protected]\",\"[email protected]\",\"[email protected]\]

I need to do this: $arrayOfEmails=json_decode($decodeEmails); But I think I need quotes around $decodedEmails for this to work. How can I add quotes around $decodeEmails string?

like image 684
Snowman Avatar asked Feb 07 '12 03:02

Snowman


4 Answers

Try this: json_decode($json_string, true);

like image 175
the_wizard Avatar answered Nov 17 '22 14:11

the_wizard


You should quote your string, it works fine, see here.

$jsonString = '["[email protected]","[email protected]","[email protected]"]';
$arrayOfEmails=json_decode($jsonString);

Or

$jsonString = "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]";
$arrayOfEmails=json_decode($jsonString);
like image 35
xdazz Avatar answered Nov 17 '22 12:11

xdazz


$data = unserialize($data) 

now u can get the $data as array

For example $data have the value like this

$data = "a:2:{s:18:"_1337666504149_149";a:2:{s:8:"fbregexp";s:1:"1";s:5:"value";s:4:"2222";}s:18:"_1337666505594_594";a:2:{s:8:"fbregexp";s:1:"3";s:5:"value";s:5:"45555";}}";

$data = unserialize($data) 

now i get value like this

Array ( [fbregexp] => 1 [value] => 2222 ) [_1337666505594_594] => Array ( [fbregexp] => 3 [value] => 45555 ) )
like image 3
kranthi kumar pulivarhty Avatar answered Nov 17 '22 14:11

kranthi kumar pulivarhty


    $str=<<<H
    {"a":"AAA","b":"333"}
    H;

     $object = json_decode($str); 
     $array  = json_decode($str , 1 );

    // $arr = get_object_vars( json_decode($str) );
like image 2
bortunac Avatar answered Nov 17 '22 14:11

bortunac