0

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

3
  • 2
    The English in this question is really unclear. Can you elaborate on what the output would be based on what conditions? Commented Jan 27, 2011 at 9:12
  • possible duplicate of [How Create Multiple array from a array ? ](stackoverflow.com/questions/4807625/…) Commented Jan 27, 2011 at 9:16
  • first,sorry with bad english. I have updated the question,Hope it more clear, Commented Jan 27, 2011 at 9:19

1 Answer 1

1
$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",
                );

$r = -1;
$move_level = array();
foreach($original as $value) {
    if(substr($value,0,2) == "03") {
        ++$r;
        $j = 0;
        $move_level[$r][$j] = '';
    } else {
        if (($j % 4) > 0) $value = ' '.$value;
        $move_level[$r][floor($j / 4)] .= $value;
        ++$j;
    }
}

echo '<pre>';
var_dump($move_level);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.