0

Having trouble with array manipulation, my array

[time] => Array
     (
       [0] => Array
          (
           [total_time] => 02:10:00
          )

       [1] => Array
          (
           [total_time] => 01:05:00
          )

       [2] => Array
          (
           [total_time] => 00:50:00
          )
  )

I wish to get

$total_time = ('02:10:00', '01:05:00', '00:50:00');

I have try with foreach

 foreach ($data['time'] as $key) {
    array_push($total_time, $key['total_time']);
 }

But my output is:

print_r($total_time) = 3

Is there any way to get this

$total_time = ('02:10:00', '01:05:00', '00:50:00');
nway
  • 29
  • 1
  • 6

1 Answers1

0

You can do it like you were doing with foreach or with array_column which is simpler:

// you could use a foreach loop like you did
$total_time = [];
foreach ($data['time'] as $key) {
    $total_time[] = $key['total_time'];
}

// or array_column
$total_time = array_column($data['time'], 'total_time');
Dharman
  • 30,962
  • 25
  • 85
  • 135