2017-10-06 124 views
0

我有這個陣列PHP數組的數組警告:非法串偏移

//this is onw part of `$pins['location']` 
array(9) { 
    ["address"]=> string(23) "11xxx Hudderfield xxxxxx" 
    ["city"]=> string(12) "Jxxxxxx" 
    ["houseNumber"]=> string(5) "11xxx" 
    ["id"]=> NULL 
    ["state"]=> string(2) "xx" 
    ["country"]=> NULL 
    ["street"]=> string(17) "Hudderfield xxxxx" 
    ["unit"]=> NULL 
    ["zip"]=> string(5) "xxxx" 
} 

的錯誤:

它的代碼時跑了我的錯誤警告一個更大的數組中:非法串偏移「地址」中/home/../../../cron.php上線77

  76 foreach ($pins['location'] as $pin_lo) { 
      77  $location_address = $pin_lo['address']; 
      78  echo $location_address; 
      79  $location_city = $pin_lo['city']; 
      80  echo $location_city; 
      81 } 

我需要能夠數組值傳遞給一個變量,你可以看到。如果我dd($ pins ['location']);它顯示了一切,因爲字符串不確定當數組在foreach後得到改變時它只會返回每一行的第一個字母或數字的任何想法?

+1

退房此[鏈接] https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php – lovelace

回答

0

問題在於你的$ pins數組的構造。在運行foreach循環時,在獲取地址值之前嘗試拋出$ pin_lo。我認爲$ pin_lo是一個字符串,而不是一個數組。

我在我的項目中使用了大量的數組,這些提示應該有助於追蹤問題。

0

這是因爲PHP沒有映射支持的功能,如Java HASH_MAP。所以你不得不自己建造它。這是你在找什麼?

<?php 
 

 
$pins = [ 
 
    "address" => "11xxx Hudderfield xxxxxx", 
 
    "city" => "Jxxxxxx", 
 
    "id" => null, 
 
    "state" => "xx", 
 
    "country" => NULL, 
 
    "street" => "Hudderfield xxxxx", 
 
    "unit" => NULL, 
 
    "zip" => "xxxx" 
 
]; 
 

 
foreach ($pins as $key => $value): 
 
    switch ($key): 
 
     case "address": 
 
      $location_address = $value; 
 
      break; 
 
     case "city": 
 
      $location_city = $value; 
 
      break; 
 
    endswitch; 
 
endforeach; 
 

 
echo "Address: ".$location_address; 
 
echo "</br>"; 
 
echo "City: ".$location_city; 
 
?>

+1

在不同的現場工作,現在會嘗試這個幾個 –

+0

事實證明它正在看每一個像它是它自己的陣列 –