2017-08-30 40 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。你只是在*顯示*的東西之前這樣做,而不是保存 –