有人可以帮助我了解此array_chunk的工作原理吗?

时间:2020-02-29 11:22:05

标签: php

为什么J的工作方式像0,2,4,1,3而不是0,1,2,3,4一样,任何人都可以提供帮助?

$data1 = ["Monday","08:00","10:00","15:00","16:00","Tuesday","08:00","10:00","18:00","21:00","Monday","11:00","12:00","17:00","20:00","Tuesday","10:00","13:00","17:00","20:00","Monday","06:00","07:00","16:00","18:00"];
$data2 = [ "Monday","08:00","09:00","18:00","20:00","Tuesday","08:00","09:00","--","--"];

$data1_chunk = array_chunk($data1,5);
$data2_chunk = array_chunk($data2,5);


for($i=0;$i<count($data2_chunk);$i++){
    for($j=0;$j<count($data1_chunk);$j++){
        if($data1_chunk[$j][0] == $data2_chunk[$i][0]){
            echo "J=>".$j."I=>".$i."\n";
        }
    }
}

输出:-

J=>0I=>0
J=>2I=>0
J=>4I=>0
J=>1I=>1
J=>3I=>1

1 个答案:

答案 0 :(得分:0)

数组块的作用是将一个大数组拆分为一个较小的数组,例如:

$array = ["test 1", "test 2","test 3", "test 4", "test 5"];
$output = array_chunk($array, 2);
var_dump($output);

分块的数组将如下所示:

[
    ["test 1", "test 2"],
    ["test 3", "test 4"],
    ["test 5"],
]