0

i have this array

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

The Error:

It is inside a much bigger array when the code is ran i get the error Warning: Illegal string offset 'address' in /home/../../../cron.php on line 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  }

I need to be able to pass the array value to a variable as you can see. If i dd($pins['location']); it show everything as strings not sure when the array is getting changed after the foreach it will only return the first letter or number of each of the lines any ideas?

episch
  • 388
  • 2
  • 19
  • 1
    check out this [link] https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php – lovelace Oct 06 '17 at 22:07

2 Answers2

0

The problem is in the construct of your $pins array. while running the foreach loop, try dumping the $pin_lo before retrieving the value of address. I think the $pin_lo is a string and not an array.

I use lots of arrays in my project, and that tips should help trace the problem.

Maccabeus
  • 89
  • 1
  • 6
0

That is because, PHP has no function for mapping support like Java HASH_MAP. So you will need to built it yourself unfortunately. Is this what are you looking for?

<?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;
?>
Prav
  • 2,785
  • 1
  • 21
  • 30