0

Im outputting some data as JSON and Im trying to replace the brackets '[' in the output with HTML tags:

$qq = json_decode($html, true);
$qq2 = str_replace("[","<span>",$qq);

print_r($qq2);

It doesn't work, meaning the brackets show up anyway and are not getting replaced. Example output:

 [2] => Array
                                (
                                    [image] => http://rack.0.mshcdn.com/james-clapper.jpg
                                    [query] => (max-width: 480px)
                                    [size] => 80x80#
                                )

I would like to replace the brackets as html tags so the output will be:

 [2] => Array
                                (
                                    <span> image </span> => http://rack.0.mshcdn.com/james-clapper.jpg
                                    <span> query </span> => (max-width: 480px)
                                    <span> size </span> => 80x80#
                                )
3
  • 1
    It appears as though you're trying to replace the text on an array not a string. us3.php.net/json_decode Commented Jan 29, 2014 at 19:02
  • Are you saying what I'm trying to do is impossible? Basically Im trying to "prettify" the json by transforming it in HTML tags. Is that not possible like this..? Commented Jan 29, 2014 at 19:06
  • 2
    As RyanS notes, $qq is an array, not a JSON string. Either way, you shouldn't be using str_replace for this. Simply iterate the array and create a string that has your formatting. Commented Jan 29, 2014 at 19:07

1 Answer 1

2

Use http://www.php.net/array_walk after you decode the string to apply str_replace to every member of the array.

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

1 Comment

Nice, if you only need to add HTML tags to the array keys, instead of using str_replace you can append and preppend them in the callback function as <span>.$key.</span>

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.