1

A JSON string is sent via a HTTP request to my webservice, it looks like this:

[\"Mairie\",\"Préfectures et sous-préfectures\"]

How can i do to make a simple php array in which i can perform php arrays functions such as implode.

I tried this but it didn't transform the JSON String into an array:

    $list=json_decode($_POST["The_JSON_Parameter"],true);

EDIT:

var_dump(json_decode($_POST["The_JSON_Parameter"], true));   
$var=json_decode($_POST["The_JSON_Parameter"],true);
$in_list = "'".implode("','", $var)."'";
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $in_list);//in the text file i got this empty string: ''
3

2 Answers 2

3
$var = "[\"Mairie\",\"Préfectures et sous-préfectures\"]";
var_dump(json_decode($var, true));

/*result:
array(2) {
  [0]=>
  string(6) "Mairie"
  [1]=>
  (33) "Préfectures et sous-préfectures"
}
*/

Works fine, please do var_dump($_POST["The_JSON_Parameter"])

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

11 Comments

But i need to store the return of the var_dump into an array to process it later, the var_dump didin't give a return.
@Malek You can just do what you did as $list=json_decode($_POST["The_JSON_Parameter"],true), var_dump is just for showing the result.
@xdazz, This is strange. This doesn't work for me with the accented letters. Do you know what php setting I need to fix?
@Walkerneo Please note that json_decode only works with UTF-8 encoded data.
@xdazz: please have a look on my EDIT.
|
0

If you try this code:

$str = "[\"Mairie\",\"Préfectures et sous-préfectures\"]";
var_dump(json_decode($str,true));

You will get:

array(2) {
  [0]=>
  string(6) "Mairie"
  [1]=>
  string(33) "Préfectures et sous-préfectures"
}

So are you sure your $_POST["The_JSON_Parameter"] has the same value i.e. a string literal "[\"Mairie\",\"Préfectures et sous-préfectures\"]" ?

Comments

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.