0

I have an array in the same level.

$original = Array(
        0=>"03ssss",//substr("03ssss",0,2)="03" => parent index

          1=>"04aaaa",
          2=>"05absd",
          3=>"07sdsa",
          4=>"08sdsd",

        5=>"03tttt", //substr("03tttt",0,2)="03" => parent index

          6=>"04xxxx, //substr("03xxxx",0,2)="04" => child index
            7=>"05sdds",
            8=>"07sdfd",
            9=>"08sdff",
          10=>"04xsax", //substr("03xxxx",0,2)="04" => child index
            11=>"05sdfs",
            12=>"07sdfds",
            13=>"08asap",
    )

How Can create from $original to multiple array something like this?

$move_level = Array(
 0=>array(0=>"04aaaa 05absd 07sdsa 08sdsd"),
 1=>array(0=>"04xxxx 05sdds 07sdfd 08sdff", 
          1=>"04xsax 05sdfs 07sdfds 08asap") 
);

thanks

4
  • 1
    Your $move_level example doesn't make sense. you want to copy the same 4 elements from $original into multiple separate arrays in the new array? Commented Jan 26, 2011 at 17:23
  • 2
    What are the rules for creating the new array? Are you simply dropping any element with a leading '03' and then chunking the remaining elements into blocks of four? Commented Jan 26, 2011 at 17:23
  • 1
    It's not totally clear what your example means, especially due to the duplicate elements in the original array. Commented Jan 26, 2011 at 17:24
  • @Matchu Not duplicate ,the input are difference . Commented Jan 27, 2011 at 8:27

1 Answer 1

1

This splits the $original array into sub arrays at each element starting with '03':

$move_level = array();
$ary = array();
foreach($original as $value) {
    if (strpos($value, '03') === 0) {
        $move_level[] = $ary;
        $ary = array();
    } else {
        $ary[] = $value;
    }
}
$move_level[] = $ary;
Sign up to request clarification or add additional context in comments.

1 Comment

@user576875 I need join "04xxxx 05xxxx 07xxxx 08xxxx" join this together Like my example output.But It differences from yours.Do you have any idea.?

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.