Martin Zeltin
Martin Zeltin

Reputation: 3198

php: How to foreach a multidimensional array?

I have been trying for a while but I can't seem to loop through a multi dimensional array.

I have this array:

$Work["work_time"] = array();
$Work["break_time"] = array();
$Work["meeting_time"] = array();
$Work["login_time"] = array();
$Work["logout_time"] = array();
$Work["work_date"] = array();

Which print_r($Work) outputs this

Array
(
    [work_time] => Array
        (
            [0] => 0.00
            [1] => 3.96
            [2] => 7.75
        )

    [break_time] => Array
        (
            [0] => 0.00
            [1] => 0.00
            [2] => 1.06
        )

    [meeting_time] => Array
        (
            [0] => 0.00
            [1] => 0.00
            [2] => 0.00
        )

    [login_time] => Array
        (
            [0] => 10:11
            [1] => 08:48
            [2] => 09:09
        )

    [logout_time] => Array
        (
            [0] => 00:00
            [1] => 13:00
            [2] => 17:59
        )

    [work_date] => Array
        (
            [0] => 2018-04-13
            [1] => 2018-04-16
            [2] => 2018-04-17
        )
)

And then I tried to use a foreach loop to loop through it and get the values but it returns nothing..

foreach ($Work as $row) {
 echo $row["login_time"];
}

What am I missing?

Upvotes: 2

Views: 2265

Answers (6)

Don't Panic
Don't Panic

Reputation: 41810

The array you're trying to iterate with your foreach loop is not the same as the array you have. In order to use that loop, your array would need to be like this instead:

[
    [
        'work_time' => 0.00,
        'break_time' => 0.00,
        'meeting_time' => 0.00,
        'login_time' => '10:11',
        'logout_time' => '00:00',
        'work_date' => '2018-04-13'
    ],
    [
        'work_time' => 3.96,
        'break_time' => 0.00,
        'meeting_time' => 0.00,
        'login_time' => '08:48',
        'logout_time' => '13:00',
        'work_date' => '2018-04-16'
    ],
    [
        'work_time' => 7.75,
        'break_time' => 1.06,
        'meeting_time' => 0.00,
        'login_time' => '09:09',
        'logout_time' => '17:59',
        'work_date' => '2018-04-17'
    ],
];

The array you have looks like the type of input you get if you have an HTML form with multiple rows of inputs with names like this:

<input type="text" name="work[work_time][]">

If that is the case, you can get an array like the one I showed above instead by switching the keys around and specifying a numeric key for each row:

<input type="text" name="work[0][work_time]">

Then it will make more sense to access the data as rows. If that's not the case, well, never mind. :)


On the other hand, with the array you have, you can just iterate one of the inner arrays directly. For example, if you wanted to show login_time values, it's just

foreach ($Work['login_time'] as $login_time) {
    echo $login_time;
}

If you need to get corresponding values from the other inner arrays, you can use the key from the array you're iterating to access those as well:

foreach ($Work['login_time'] as $key => $login_time) {
    echo 'login time: ' . $login_time . ', work_date: ' . $Work['work_date'][$key];
}

Upvotes: 3

Derek
Derek

Reputation: 3435

If you already know the key you need (login_time), you do not need to iterate the array. You can access it directly as $Work["login_time"].

If you want to iterate $Work["login_time"], you can use foreach:

foreach ($Work['login_time'] as $row) {
   echo $row;
}

Upvotes: 1

Charis Moutafidis
Charis Moutafidis

Reputation: 363

If you really dont know how many dimensions you are dealing with, a recursive solution is what you need. Here is my code:

$Work["work_time"] = array(1, 2, 3);
$Work["break_time"] = array(4, 5, 6);
$Work["meeting_time"] = array(7, 8, 9);
$Work["login_time"] = array(10, 11, 12);
$Work["logout_time"] = array(13, 14, 15);
$Work["work_date"] = array(16, 17, 18);

function forLoop($array){
    foreach ($array as $row) {
        if(is_array($row)){
            forLoop($row);
        }
        else{
            echo $row;
            echo " ";
        }
    }
}

forLoop($Work);

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 

You actually need to make a formating or something to make it work the way you want it to. Edit: forLoop function calls it's self so it will work on any dimension array. Tell me what you think!

Upvotes: 3

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

foreach ($dataArray as $innerArray){
   foreach ($innerArray as $key=>$value){
   // here is where you access the array -- $value
   }
}

Upvotes: 0

CharlesEF
CharlesEF

Reputation: 628

To loop completely you would need a 2nd foreach inside the first foreach. Like This:

 foreach ($Work as $row) {
  foreach ($row as $key => $value) {
  echo "$key - $value";
 }
}

Upvotes: 0

Aman jaura
Aman jaura

Reputation: 201

Probably need multiple for loops

foreach ($Work as $row) {
   foreach($row as $key => $rs) {
       echo $rs;
  }
}

Upvotes: 0

Related Questions