Gislef
Gislef

Reputation: 1637

Foreach Urls JSON Decode

$urlsDB is a variable containing JSON.

print_r($urlsDB );

outputs:

[\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\",\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"]

How do I create the foreach using json_decode correctly?

   <?php
    $urls = json_decode($urlsDB , true);

    if ($urls != '' ) { 

            foreach ($urls as $url) {
    ?>
    <img src="<?php echo $url;?>"  class="img-responsive img-thumbnail " />

    <?php
     }
    }

    ?>

Var_dump($urls); returns empty.

Upvotes: 1

Views: 204

Answers (2)

William Perron
William Perron

Reputation: 1398

Try stripping the slashes before the json_decode()

$input = '[\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\",\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"]';

$json = json_decode(stripslashes($input),true);

the output of var_dump($json) gives me the following

array(2) {
    [0]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/LOGO-SEG-2.png"
    [1]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/algoritims.jpg"
}

Try it here!


Edit to add:

the original string is really a string representation of a json array, and not an object since it has no keys. I tried that same fix on an object with a url key on each element and this is is the result.

$input = '[{\"url\" : \"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\"},{\"url\" : \"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"}]';

$json = json_decode(stripslashes($input),true);

Outputs:

array(2) {
    [0]=> array(1) {
        ["url"]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/LOGO-SEG-2.png"
    }
    [1]=> array(1) {
        ["url"]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/algoritims.jpg"
    }
}

Additionally, \" is not a valid json character in a string literal. It is only valid if the variable is declared using " (ex: $encoded = "{\"some value\"}";).

echo var_dump(json_decode('{\"myKey\": \"myValue\"}', true)); // output: NULL

Try it live.

Upvotes: 0

ishegg
ishegg

Reputation: 9937

You are getting invalid JSON. You have escaped quotes at the beginning and end. This is valid:

["http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png","http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg"]

Maybe you're applying some filter to the database before processing it?

This should be working:

<?php
$a = '["http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png","http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg"]
';
$urls = json_decode($a, true);
if (count($urls) > 0) {
    foreach ($urls as $url) {
    ?>
        <img src="<?php echo $url;?>"  class="img-responsive img-thumbnail " />
    <?php
    }
}

You shouldn't htmlspecialchars() the JSON encoded string. That will escape the quotes, making the JSON object invalid. What you should do is htmlspecialchars() every element in the array separately, preferably at the moment of display, not at the moment of saving it (read here).

Upvotes: 1

Related Questions