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() 
5
  • 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) Commented Sep 5, 2017 at 14:47
  • 2
    $row["Date"] is a string, foreach doesn't complain for nothing. Commented Sep 5, 2017 at 14:48
  • just do echo $row["Date"] - no need for the nested foreach if you just want the date. Commented Sep 5, 2017 at 14:49
  • @Calimero Yes structure is same for all arrays Commented Sep 5, 2017 at 14:49
  • Possible duplicate of Make a unique list of values from a particular key existing anywhere in a deep array ... of course, you can omit the final step of removing duplicates if you wish. Commented Sep 8, 2017 at 2:45

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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
}

3 Comments

How can i use the isset on date. Date is returning as a string.
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.
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
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.

2 Comments

I am getting an undefined index error for Date. Is there something i am missing. Date is a string
@jumpman8947 You shouldn't get that warning if the structure of all arrays is the same as you showed.
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.

4 Comments

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
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
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
My array structure is as posted above but i am still getting undefined index: Date error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.