2017-08-30 63 views
1

$urlsDB是一个包含JSON的变量。Foreach Urls JSON解码

print_r($urlsDB); 

输出:

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

如何创建正确使用json_decodeforeach

<?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);返回空。

回答

0

尝试之前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); 

剥离斜线var_dump($json)输出给了我下面的

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" 
} 

试试吧here


编辑补充:

原始字符串是一个真正的JSON数组的字符串表示,而不是一个对象,因为它没有密钥。我在每个元素上使用url键的对象上尝试了同样的修复,这就是结果。

$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); 

输出:

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" 
    } 
} 

此外,\"在字符串文本中一个有效的JSON字符。只有在变量声明为"(例如:$encoded = "{\"some value\"}";)时才有效。

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

试试吧live

+0

这是治疗症状,而不是原因。另外,当任何元素包含双引号时它会中断。 – ishegg

+0

@ishegg我不同意:“json_decode()”不起作用的原因是由于反斜杠(证明:你的答案中的字符串与OP的反斜线存在唯一的区别),我们也不知道在哪里OP从这些数据中获得,因为我们知道他/他可能无法控制输入值 –

+1

这绝对是因为反斜杠。但问题在于'\“'是JSON对象中的一个有效字符串,所以,如果你将它们去掉,你将*在将来遇到问题(即URL带有文本描述,这可能是完美的引用它)。看看[这里](https://3v4l.org/1gqSB)。完全有效的字符串,但代码因'stripslashes()'为JSON编码的字符串而中断 – ishegg

1

您正在收到无效的JSON。你在开始和结束时已经逃脱了引号。这是有效的:

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

也许你在处理它之前对数据库应用了一些过滤器?

这应该是工作:

<?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 
    } 
} 

你不应该htmlspecialchars()的JSON编码字符串。这将会导致引号无效,从而导致JSON对象无效。你应该做的是htmlspecialchars()阵列中的每个元素分开,最好在显示的那一刻,而不是保存它的时刻(阅读here)。

+0

保存之前我使用'htmlspecialchars',因为我无法在输入中保存数组,因此我将该数组转换为JSON – Gislef

+1

您使用**编码后的htmlspecialchars()**到JSON?你应该在编码之前进行编码。那么你应该没有任何问题。或者,甚至更好,直到解码之后才打印每个元素。 – ishegg

+1

甚至更​​好的解决方案是根本不使用htmlspecialchars。你只是在*显示*的东西之前这样做,而不是保存 –