-6
$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>';

OUTPUT:

/*

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 
        )

)

*/

Everybody here have any idea with the function to get the this result.

/*

   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 
        )

)


*/
2

3 Answers 3

1

Two changes:

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
    }
}

See it

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that you understand my questions and give me a good answers.100% agree
Why it still got the duplicate element ?Did you have any idea?
0

You can do that with array_slice and 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))
);

You should be able to use similar code to this in your for-loop if you want to do it for a dynamic array

Comments

0

what you can do is trim the first 3 characters like substr($val, 3); and past the 03a before it.. or do you wanna get the first 3 characters from array index 0 and past/replace it in index 1?? if thats the case do like:

$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);

result:

Array
(
    [0] => Array
    (
        [0] => 03A04a05b05b06c06c07d07d //yes "03A" here 
        [1] => 03A04w05x05x06y06y07z07z //yes "03A" here 
    )

    [1] => Array
    (
        [0] => 03B04l05m05m06n06n07o07o //"03B" here 
        [1] => 03B04l05m05m06n06n07o07o//"03B" here 
    )

)

Im sure there is a better way, this is a global way i think

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.