jumpman8947
jumpman8947

Reputation: 581

Multidimensional array loop to get value

I have an array of multiple arrays all with different levels. What im trying to do is loop though the array by key, and it will go through each level getting the values for those keys. myArray looks something like this

Array ( [0] => 
  Array ( [Date] => 2011-15-22 
          [Color] => blue
          [Status] => Fresh 
        [1] => 
  Array ( [Date] => 1999-08-04 
          [Color] => green
          [Status] => Rotten) )

I have tried

foreach($myArray as $row){
  foreach($row["Date"] as $k){
     echo $k
    } 
 }

I am getting an

Notice: Undefined index: Date 

and

Warning: Invalid argument supplied for foreach() 

Upvotes: 0

Views: 638

Answers (5)

Abraham Romero
Abraham Romero

Reputation: 1117

On your foreach, you should specify the key and value so you can access both:

foreach ($myArray as $key => $value){
    echo $key.' is '. gettype ($value).'<br>';
    if (is_array($value)){
        foreach ($value as $subKey => $subValue){
            echo $subkey . ' => ' . $subValue . '<br>';
        }
    }
}

This way you can access and print all values without losing the structure

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

Simply with array_walk_recursive function:

$arr = [
    [ 'Date' => '2011-15-22', 'Color' => 'blue', 'Status' => 'Fresh' ],
    [ 'Date' => '1999-08-04', 'Color' => 'green', 'Status' => 'Rotten' ]
];

array_walk_recursive($arr, function($v, $k){
    if ($k == 'Date') echo $v . PHP_EOL;
});

The output:

2011-15-22
1999-08-04

Upvotes: 2

Akshay Hegde
Akshay Hegde

Reputation: 16997

Warning: Invalid argument supplied for foreach()

Because $row["Date"] is string

foreach() - foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Notice: Undefined index: Date

Probably your array may not have element with key Date somewhere (your array structure probably different, as iteration takes place), so you are getting this message, use isset() to or array_key_exists() to validate, depending on the purpose.

Please note isset != array_key_exists

$a = array('key1' => 'test1', 'key2' => null);
isset($a['key2']);             // false
array_key_exists('key2', $a);  // true

There is another important difference. isset doesn't complain when $a does not exist, while array_key_exists does.

Upvotes: 0

TecBrat
TecBrat

Reputation: 3729

It looks like you just need this:

foreach($myArray as $row){
  echo $row["Date"]; 
 }

or

foreach($myArray as $row){
  $k= $row["Date"]; 
  //do stuff...
 }

or

foreach($myArray as $row){
  $k[]= $row["Date"]; 
 }
// do stuff with $k[] array.

Upvotes: 0

D B
D B

Reputation: 532

As axiac states in the comments, $row["Date"]is a String and therefore not iterable. You probably just want this:

foreach($myArray as $row){
  foreach($row as $k){
     echo $k
    } 
 }

The Notice Undefined index: Date also describes what is going wrong - you are accessing the index without checking if it exists. It does look like that your data structure is not always the same. In this case you should always check the existence with the isset function:

if (isset($row["Date"])) {
    //do something here
}

Upvotes: 0

Related Questions