How Create multiple array from an array and concatenate each elments?
I have an array in the same level.
$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",
)
base on $original
if(substr("03ssss",0,2)="03") Will be came the index of main array
else will the index of child array andconcat with these elements with if we found substr("03ssss",0,2)="04"
So the expected result look like:
$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"
)
);
If I try:
$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 :
Array (
[0] => Array
(
[0] => 04xxxx 05xxxx 07xxxx 08xxxx
)
[1] => Array
(
[0] => 04xxxx 05xxxx 07xxxx 08xxxx,
[1]=>04xxxx 05xxxx 07xxxx 08xxxx
)
)
How Can create from $original to multiple array something like this?
thanks