0

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() 
jumpman8947
  • 571
  • 1
  • 11
  • 34
  • Are you *only* getting these errors ? Are you certain the array structure is exactly the same for all elements (figuring you have a lot more than 2 items) – Calimero Sep 05 '17 at 14:47
  • 2
    `$row["Date"]` is a string, `foreach` doesn't complain for nothing. – axiac Sep 05 '17 at 14:48
  • just do `echo $row["Date"]` - no need for the nested foreach if you just want the date. – Bananaapple Sep 05 '17 at 14:49
  • @Calimero Yes structure is same for all arrays – jumpman8947 Sep 05 '17 at 14:49
  • Possible duplicate of [Make a unique list of values from a particular key existing anywhere in a deep array](https://stackoverflow.com/questions/25634515/make-a-unique-list-of-values-from-a-particular-key-existing-anywhere-in-a-deep-a) ... of course, you can omit the final step of removing duplicates if you wish. – mickmackusa Sep 08 '17 at 02:45

5 Answers5

2

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
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

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

Abraham Romero
  • 1,047
  • 11
  • 22
0

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.

Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • I got rid of the second foreach but the i am getting the undefined index error. Date is a string how can i fix this error – jumpman8947 Sep 05 '17 at 14:56
  • Printing the result of the foreach after adding an if isset statment returns nothing. But if i dynamically return myArray[0]["Date"] it will return content – jumpman8947 Sep 05 '17 at 15:04
  • isset or array_key_exists is for validation if required, if your array is same as above this `foreach($myArray as $row){ echo $row['Date']; }` should work – Akshay Hegde Sep 05 '17 at 15:08
  • My array structure is as posted above but i am still getting undefined index: Date error – jumpman8947 Sep 05 '17 at 15:15
0

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
}
D B
  • 534
  • 3
  • 14
  • How can i use the isset on date. Date is returning as a string. – jumpman8947 Sep 05 '17 at 14:54
  • `isset` just does one thing: It tells you if a variable is `null` (which every variable is by default) or not. It does not matter what you pass in there. – D B Sep 05 '17 at 14:58
  • Printing the result of the foreach after adding an if isset statment returns nothing. But if i dynamically return myArray[0]["Date"] it will return content – jumpman8947 Sep 05 '17 at 15:06
0

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.
TecBrat
  • 3,643
  • 3
  • 28
  • 45