8

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?

4
  • it seems like you have to escape the quotes inside the string for it to be valid: "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]" Commented Feb 7, 2012 at 3:35
  • This is the Objective C version of the string..it gets sent to PHP via POST Commented Feb 7, 2012 at 3:36
  • You need to define "doesn't work." json_decode() is the proper method for this purpose, so what are you getting when you use it? Commented Feb 7, 2012 at 3:38
  • I edited it now, that's the actual PHP string. Do I have to do anything to it first? Commented Feb 7, 2012 at 3:39

9 Answers 9

10

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);
Sign up to request clarification or add additional context in comments.

4 Comments

@mohabitar If you are already escape the ", then you can use ".
var_dump gives this (The actual array has more than 3, I took the extras off from here just so I can show it): string(457) "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]"
@mohabitar It's already a string, you can just use json_decode after it .
But that's what I'm doing isn't it? $decodeEmails=$_POST["arrayOfEmails"]; $arrayOfEmails=json_decode($decodeEmails);
10

Try this: json_decode($json_string, true);

5 Comments

This is correct, the other answers don't seem to know about the second argument for json_decode() and are only type-casting the first level elements.
But this returns an associative array, no? I just want a regular
@mohabitar, PHP only has one type of array. I think you mean that the array keys are not numeric then? Your input is what needs to be checked or corrected to get the output you desire.
There is no need for the second param in thie case given in the question. The second param is for converting JSON serialized objects to assoc arrays. The OP has a JSON serialized indexed array.
When I passed second parameter assoc= true to json_decode() its worked but I have to called it 2 times to achive my output
3
$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 ) )

Comments

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

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

    // $arr = get_object_vars( json_decode($str) );

Comments

1

If json_decode isn't working, you could try something like this:

$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 3 ) );

1 Comment

Worked for me as I was passing JSON string in URL
1

You could use json_decode() then print_r() to create a PHP formatted array

<?php
$json = file_get_contents('json/yourscript.json'); // Get the JSON data
$phpObj = json_decode($json,true);  // Convert to PHP Object
print_r($phpObj);  // Convert to PHP Array
?>

Comments

0

This code works fine.

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

4 Comments

you don't have to escape the quotes in this case.
Instead of type-casting, you should be using the second argument to convert to an array: json_decode($value, $array = TRUE).
Yes. I just tried to fix his error. Here is my result. Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] )
There is no need to typecast, or to pass the second param as true
0

Assuming the lack of quotes around your JSON in the question is a transposition error during posting, then the code you're using is fine: http://codepad.org/RWEYM42x

You do need to ensure your string is UTF8 encoded. You can use the built in encoder if it isn't ( http://php.net/manual/en/function.utf8-encode.php ).

For any further help, you need to actually tell us what you are getting with your code.

Comments

-2
$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 3 ) )

This solution is good but for getting full valid array I'm using strlen( $json ) - 6, so it should be:

$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 6 ) );

var_dump($arr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.