简体   繁体   中英

Can Anyone Help Me To Understand This array_chunk Working?

why J is working like 0,2,4,1,3 instead of 0,1,2,3,4 anyone can help ?

$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";
        }
    }
}

output:-

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

What array chunk does is split one big array into an array of smaller arrays, example:

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

The chunked array will look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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