1

I have below string. I need to convert this to an associative array. The string which I am getting, as a result, is coming from ajax and for this format of string I didn't find any solutions.

$string= '[{"name":"title","value":"%post_title%"},{"name":"author","value":"%author_name%"}]';

I need to convert the above string to an associative array so that I can use that array in foreach.

3
  • 3
    That is a JSON String so use $array = json_decode($string,1); The manual Commented Oct 13, 2018 at 9:28
  • I did not find any solutions given for this format of the string. I found the same question, but the format of the string was different there. @RiggsFolly Commented Oct 13, 2018 at 9:37
  • Check the json.org site to familiarize yourself with what JSON looks like. It is used for many things nowadays Commented Oct 13, 2018 at 9:39

1 Answer 1

5

Use json_decode with second parameter set to true, in order to convert your input JSON format string to an associative array.

$string= '[{"name":"title","value":"%post_title%"},{"name":"author","value":"%author_name%"}]';

// convert the JSON format to array
$output_array = json_decode($string, true);

// print to check output
echo "<pre>"; print_r($output_array); echo "</pre>";
Sign up to request clarification or add additional context in comments.

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.