0
如何從數組中創建多個數組並連接每個元素?我有一個在同一級別的數組。如何從數組中創建多個數組並連接每個元素?
$original = Array(
0=>"03ssss",//first "03"
1=>"04aaaa",//first "04" of the first "03"
2=>"05absd",
3=>"07sdsa",
4=>"08sdsd",
5=>"03tttt", //second "03"
6=>"04xxxx, //first "04" of the second "03"
7=>"05sdds",
8=>"07sdfd",
9=>"08sdff",
10=>"04xsax", //second "04" of second "03"
11=>"05sdfs",
12=>"07sdfds",
13=>"08asap",
)
基礎上$original
if(substr("03ssss",0,2)="03")
公司將來到主數組的索引
else
將孩子數組的索引和concat
用,如果我們發現substr("03ssss",0,2)="04"
所以這些元素預計result
看起來像:
$move_level = Array(
0=>array(// first "03"
0=>"04aaaa 05absd 07sdsa 08sdsd"),//first 04 of the first "03"
1=>array(// second "03"
0=>"04xxxx 05sdds 07sdfd 08sdff", // Concat with "04"
1=>"04xsax 05sdfs 07sdfds 08asap" // Concat with "04"
)
);
如果我嘗試:
$move_level = array(); //main array
$ary = array(); // sub array
foreach($original as $value) {
if (substr($value,0,2) =="03") {
$move_level[] = $ary;// create main array
$ary = array();// create sub
} else { // to to join all element with "04"
$ary[] = ($value);
}
}
$move_level[] = $ary;
echo '<pre>';
print_r($move_level);
echo '</pre>';
OUTPUT:
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => 04xxxx
[1] => 05xxxx
[2] => 07xxxx
[3] => 08xxxx
)
[2] => Array
(
[0] => 04xxxx
[1] => 05xxxx
[2] => 07xxxx
[3] => 08xxxx
[4] => 04xxxx
[5] => 05xxxx
[6] => 07xxxx
[7] => 08xxxx
)
)
I WANT GET:
陣列 (
[0] => Array
(
[0] => 04xxxx 05xxxx 07xxxx 08xxxx
)
[1] => Array
(
[0] => 04xxxx 05xxxx 07xxxx 08xxxx,
[1]=>04xxxx 05xxxx 07xxxx 08xxxx
)
)
如何從創建到多個數組這樣的東西? 謝謝
在這個問題上的英語真的不清楚。你能否詳細說明在什麼條件下產出會是什麼? – Merijn 2011-01-27 09:12:17
[如何從數組創建多個數組? ](http://stackoverflow.com/questions/4807625/how-create-multiple-array-from-a-array) – 2011-01-27 09:16:40