1

RESOLVED: I WAS ADDING EXTRA INDEX TO $firms LIKE $firms['type'] = 'ABC' AS $firms Already Contains CHILD ARRAYS. My BAD, Sorry.

This might be duplicate But i couldn't figure it out T _ T Sorry

As i am printing array results coming right from database, its continuously annoying me with error

Illegal string offset 'id'

When i simply print_r result array it gives me following result.

echo "<pre>";
   foreach ($firms as $firm) {
       print_r($firm[0]);
   }
echo "</pre>";

Result:

Array
(
    [id] => 7923
    [rank] => 0
    [name] => CBRE
)
Array
(
    [id] => 7919
    [rank] => 0
    [name] => Aecom
)

But if i try to print 'id' it gives me error:

echo "<pre>";
    foreach ($firms as $firm) {
        echo $firm[0]['id'];
    }
echo "</pre>";

Error:

Illegal string offset 'id'

VAR_DUMP($firms) , Result:

array(3) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      ["id"]=>
      int(7923)
       ["rank"]=>
      int(0)
       ["name"]=>
       string(4) "CBRE"
   }
 }
 [1]=>
 array(1) {
   [0]=>
    array(3) {
      ["id"]=>
       int(7919)
       ["rank"]=>
       int(0)
       ["name"]=>
       string(5) "Aecom"
     }
  }
  [2]=>
   array(1) {
    [0]=>
     array(3) {
      ["id"]=>
      int(6793)
      ["rank"]=>
      int(1)
      ["name"]=>
      string(5) "AECOM"
   }
  }
}
Noman Ali
  • 3,160
  • 10
  • 43
  • 77

0 Answers0