简体   繁体   English

如何在php中获取上一个和下一个数组值?

[英]How to get previous and next array value in php?

So I am using a RapidAPI's API where I am getting this below array after json_encode() it -所以我使用的是 RapidAPI 的 API,在 json_encode() 之后我在数组下面得到它 -

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 Ie [00:00:00] and not the ending time ie it should have been [00:30:00] the end time.现在你已经看到了我从现在开始得到的结构,请注意我只得到开始时间,即[00:00:00]而不是结束时间,即它应该是[00:30:00]结束时间。

But using但是使用

foreach ($arr as $key => $value) { 
}

I am getting我正进入(状态

  • [00:00:00] [00:00:00]
  • [00:30:00] [00:30:00]

respectively as you can expect in foreach I have tried using foreach inside foreach using array_slice but failed.分别正如您在 foreach 中所期望的那样,我尝试在 foreach 中使用 array_slice 使用 foreach 但失败了。

So what I want is所以我想要的是

  • Start Time - End Time开始时间 - 结束时间
  • [00:00:00] [00:30:00] [00:00:00] [00:30:00]
  • [00:30:00] ..... [00:30:00] .....

You might take and loop the keys from the array using array_keys.您可以使用 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看一个PHP 演示

I hope this is what you're looking for:我希望这是你正在寻找的:

<?php

// sample data
$data = [
    
    "00:00:00" => [
        "name" => "Test name",
        "details" => "Test details",
        "type" => "Test type"
    ],
    
    "00:30:00" => [
        "name" => "Test name",
        "details" => "Test details",
        "type" => "Test type"
    ],
    
    "00:60:00" => [
        "name" => "Test name",
        "details" => "Test details",
        "type" => "Test type"
    ]
    
];

$timeslots = [];

foreach(array_chunk($data, 2, TRUE) as $key => $value) {
    
    $timeslots[] = array_keys($value);
}

print_r($timeslots);

?>

Output:输出:

Array
(
    [0] => Array
        (
            [0] => 00:00:00
            [1] => 00:30:00
        )

    [1] => Array
        (
            [0] => 00:60:00
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM