这是我的print_t($ array)
的结果[0] => 1/000000.png
[1] => 1/0000FF.png
[2] => 1/66CC00.png
[3] => 1/CC66FF.png
[4] => 1/FF0000.png
[5] => 1/FF9900.png
[6] => 1/FFFFFF.png
[7] => 2/000000.png
[8] => 2/0000FF.png
[9] => 2/66CC00.png
[10] => 2/CC66FF.png
[11] => 2/FF0000.png
[12] => 2/FF9900.png
[13] => 2/FFFFFF.png
[14] => 3/000000.png
[15] => 3/0000FF.png
[16] => 3/66CC00.png
[17] => 3/CC66FF.png
[18] => 3/FF0000.png
[19] => 3/FF9900.png
[20] => 3/FFFFFF.png
我需要对该数组进行分组以获得这种结果:
arr[0] = "000000.png, 000000FF.png, 66CC00.png, CC66FF.png, FF0000.png, FF9900.png, FFFFFF.png"
arr[1] = "000000.png, 000000FF.png, 66CC00.png, CC66FF.png, FF0000.png, FF9900.png, FFFFFF.png"
arr[2] = "000000.png, 000000FF.png, 66CC00.png, CC66FF.png, FF0000.png, FF9900.png, FFFFFF.png"
arr[3] = "000000.png, 000000FF.png, 66CC00.png, CC66FF.png, FF0000.png, FF9900.png, FFFFFF.png"
问候
答案 0 :(得分:1)
因为你需要一个字符串
foreach ($array as $value) {
$result[dirname($value)] .= empty($result[dirname($value)]) ? basename($value) : ', ' . basename($value);
}
答案 1 :(得分:0)
试试这个!
$array = ["1/000000.png", "1/0000FF.png", "1/66CC00.png", "2/000000.png", "2/0000FF.png", "2/66CC00.png", "3/000000.png", "3/0000FF.png", "3/66CC00.png"];
//Get the first group of array
for($i=0; $i<count($array); $i++){
if(!$i){
$new_array[] = substr($array[$i], 2);
$firstChar = $array[$i][0];
}else{
if($firstChar == $array[$i][0]){
$new_array[] = substr($array[$i], 2);
}else{
break;
}
}
}
//Array string
$array_string = implode(",", $new_array);
//Calculate the new length of array
$final_array_length = count($array)/$i;
//Prepare final array
$final_array = [];
for($j=0; $j<$final_array_length; $j++){
$final_array[] = $array_string;
}
print_r($final_array);