簡體   English   中英

遍歷數組:PHP

[英]looping through an array in : PHP

我的JSON數組:

[\"{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx5,78.xxxxxx3\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":1,\\\"reciever\\\":\\\"+91xxxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}\",\"{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx,78.xxxxx\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":2,\\\"reciever\\\":\\\"+91xxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}\"]

我將這個JSONArray作為POST請求的一部分傳遞給add Parameter。

我的代碼用於顯示數組的內容

$jsonData = stripslashes($_POST['add']);
$phpArray = json_decode($jsonData,true);
foreach ($phpArray as $index => $record) 
{
 echo $record["caller"];
}

我的輸出僅是兩個大括號。

{{

我不明白怎么了

您的JSON無效,每個數組不應包含引號。

修復生成的內容,如果不能,則將修復以下內容

$jsonData = stripslashes(stripslashes($jsonData));
$jsonData = str_replace(
  array('"{', '}"'),
  array('{', '}'),
  $jsonData
);

$phpArray = json_decode($jsonData,true);
foreach ($phpArray as $index => $record) 
{
   echo $record["caller"];
}

雖然作為最后的手段,但最好在發送前修復損壞的JSON。 魔術引號也已從PHP中刪除。 避免使用它

以上輸出

+ 91xxxx + 91xxxxx

您的json字符串有問題。 嘗試這個 :

$post = '[{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx5,78.xxxxxx3\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":1,\\\"reciever\\\":\\\"+91xxxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"},{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx,78.xxxxx\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":2,\\\"reciever\\\":\\\"+91xxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}]';

$jsonData = stripslashes(stripslashes($post));
$phpArray = json_decode($jsonData,true);
foreach ($phpArray as $index => $record) 
{
echo $record["caller"];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM