phparraysmultidimensional-array

How to access a column value in a 2d array?


I want to echo the array value with the key of type , here is my code,

foreach ($sam as $key => $sa) {
    foreach ($sa as $s) {
        echo $s['type'];
    }
}

and this is the content of array $sam:

array (size=1)
  0 => 
    array (size=5)
      'type' => string 'days' (length=4)
      'bookable' => string 'no' (length=2)
      'priority' => int 10
      'from' => string '1' (length=1)
      'to' => string '1' (length=1)

I don't understand why my series of foreach calls results in

Warning: Illegal string offset 'type'

Can you help me echo the type values?


Solution

  • You only need one foreach:

    foreach ($sam as $s) {
        echo $s['type'];
    }