$original = Array(
0=>"03A ",//first "03", substr("04w",0,2)=="03"
1=>"04a ",//first "04" of first "03"
2=>"05b ",
3=>"06c ",
4=>"07d ",
6=>"04w ",//second "04" of first "03"
7=>"05x ",
8=>"06y ",
9=>"07z ",
10=>"03B ",//second "03"
11=>"04l ",//first "04" of second "03" //substr("04w",0,2)=="03"
12=>"05m ",
13=>"06n ",
14=>"07o ",
15=>"04l ",//second "04" of second "03" //substr("04l",0,2)=="03"
16=>"05m ",
17=>"06n ",
18=>"07o "
);
echo '<pre>';
print_r($original);
echo '</pre>';
function customize_array($original){
$customized_array = array();
foreach($original as $value) {
if(substr($value,0,2) == "03") {
// CHANGE 1
$save = $value;// saved "03" yo put in child too.
++$r;//inc parent
$j = 0; // child
$customized_array[$r][$j] =$value; // create parent
} else {
if (($j % 4) > 0) $value .= $value;
// CHANGE 2
else if ($j && $j % 4 == 0) $value = $save.$value;
$customized_array[$r][floor($j / 4)] .= $value;
++$j;//inc child
}
}
return $customized_array;
}
$display_list = customize_array($original);
echo '<pre>';
print_r($display_list);
echo '</pre>';
输出:
/*
Array
(
[1] => Array
(
[0] => 03A 04a 05b 05b 06c 06c 07d 07d
[1] => 03A 04w 05x 05x 06y 06y 07z 07z
)
[2] => Array
(
[0] => 03B 04l 05m 05m 06n 06n 07o 07o
[1] => 03B 04l 05m 05m 06n 06n 07o 07o
)
)
* /
这里的每个人都有任何想法来获得这个结果。
/*
Array
(
[1] => Array
(
[0] => 03A 04a 05b 06c 07d
[1] => 03A 04w 05x 06y 07z
)
[2] => Array
(
[0] => 03B 04l 05m 06n 07o
[1] => 03B 04l 05m 06n 07o
)
)
*/
答案 0 :(得分:1)
两个变化:
foreach($original as $value) {
if(substr($value,0,2) == "03") {
// CHANGE 1
$save = $value;
++$r;//inc parent
$j = 0; // child
$move_level[$r][$j] =$value;
} else {
if (($j % 4) > 0) $value .= $value;
// CHANGE 2
else if ($j && $j % 4 == 0) $value = $save.$value;
$move_level[$r][floor($j / 4)] .= $value;
++$j;//inc child
}
}
答案 1 :(得分:0)
您可以使用array_slice和implode执行此操作:
$a1 = array_slice($original, 0, 5);
$a2 = array_slice($original, 5, 4);
$a3 = array_slice($original, 10, 5);
$a4 = array_slice($original, 15, 4);
$output = new Array(
new Array(implode("",$a1), implode("",$a2)),
new Array(implode("",$a3), implode("",$a4))
);
如果要为动态数组执行此操作,您应该能够在for循环中使用类似的代码
答案 2 :(得分:0)
你可以做的是修剪前3个字符,如substr($val, 3);
,然后经过03a。
或者你想从数组索引0获取前3个字符并在索引1中过去/替换它?
如果是这样的话:
$array[0] => Array
(
[0] => 03A04a05b05b06c06c07d07d //yes "03A" here
[1] => 04w05x05x06y06y07z07z // no "03A" here
)
$array[1] => Array
(
[0] => 03B04l05m05m06n06n07o07o //"03B" here
[1] => 04l05m05m06n06n07o07o // no "03B" here
)
$new_array = array();
foreach($array as $value)
{
$first = substr($value[0], 0, 3);
$new_array[] = array($value[0], $value[1]);
}
print_r($new_array);
结果:
Array
(
[0] => Array
(
[0] => 03A04a05b05b06c06c07d07d //yes "03A" here
[1] => 03A04w05x05x06y06y07z07z //yes "03A" here
)
[1] => Array
(
[0] => 03B04l05m05m06n06n07o07o //"03B" here
[1] => 03B04l05m05m06n06n07o07o//"03B" here
)
)
我确信有更好的方法,这是我认为的全球方式