So I am using a RapidAPI's API where I am getting this below array after json_encode() it -
Array
(
[00:00:00] => Array
(
[name] => Arr Name 1
[other-details] => Arr Desscription 1
[type] => Arr Type 1
)
[00:30:00] => Array
(
[name] => Arr Name 2
[other-details] => Arr Desscription 2
[type] => Arr Type 2
)
)
Now that you have seen the structure I am getting from now on please notice that I am getting only the starting time I.e [00:00:00] and not the ending time i.e it should have been [00:30:00] the end time.
But using
foreach ($arr as $key => $value) {
}
I am getting
respectively as you can expect in foreach I have tried using foreach inside foreach using array_slice but failed.
So what I want is
You might take and loop the keys from the array using array_keys.
In the loop, print the starting time, and only print the ending time if it exist by checking if the key for the next value in the array exists.
$a = [
'00:00:00' => [
'name' => 'Arr Name 1',
'other-details' => 'Arr Desscription 1',
'type' => 'Arr Type 1'
],
'00:00:30' => [
'name' => 'Arr Name 2',
'other-details' => 'Arr Desscription 2',
'type' => 'Arr Type 2'
],
'00:01:00' => [
'name' => 'Arr Name 3',
'other-details' => 'Arr Desscription 3',
'type' => 'Arr Type 3'
],
'00:01:30' => [
'name' => 'Arr Name 4',
'other-details' => 'Arr Desscription 4',
'type' => 'Arr Type 4'
]
];
$keys = array_keys($a);
for ($i = 0; $i < count($keys); $i++) {
$result = $keys[$i];
if (array_key_exists($i+1, $keys)) {
$result .= " " . $keys[$i + 1];
}
echo $result . PHP_EOL;
}
Output
00:00:00 00:00:30
00:00:30 00:01:00
00:01:00 00:01:30
00:01:30
See a PHP demo