Input Array : Input array is like below and level in it can be dynamic up to any level,
$array_input = [ 0 => ["level" => "L1", "points" => 1000], 1 => ["level" => "L1", "points" => 5000], 2 => ["level" =>"L2 ", "points" => 3000], 3 => ["level" => "L3", "points" => 4000], 4 => ["level" => "L3", "points" => 6000], 5 => ["level" => "L2", "points" => 4000], 6 => ["level" => "L2", "points" => 5000], 7 => ["level" => "L2", "points" => 5000], 8 => ["level" => "L1", "points" => 6000], 9 => ["level" => "L1", "points" => 2000] ];
OUTPUT ARRAY: Output array should be like below, I tried using 2 foreach loop and accessing previous Level and compare it to next Level. But it is creating problem where Level is more than 2 times.
$array_output = [ 0 => [ 0 => ["level" => "L1", "points" => 1000], 1 => ["level" => "L1", "points" => 5000] ], 1 => [ 0 => ["level" => "L2", "points" => 3000] ], 2 => [ 0 => ["level" => "L3", "points" => 4000], 1 => ["level" => "L3", "points" => 6000] ], 3 => [ 0 => ["level" => "L2", "points" => 4000], 1 => ["level" => "L2", "points" => 5000], 2 => ["level" => "L2", "points" => 5000] ], 4 => [ 0 => ["level" => "L1", "points" => 6000], 1 => ["level" => "L1", "points" => 2000] ] ];
This is the code i tried but it’s not giving result what i want:
$opArr = []; $i=1; $justArr = []; $pre = $array_input[0]['level']; $firstArr = $array_input[0]; for($i; $i<count($array_input); $i++){ // 0 foreach($array_input[$i] as $key => $value){ if($key == 'level'){ $level = $value; } } if($pre == $level ){ $opArr[] = $firstArr; $opArr[] = $array_input[$i]; } $pre = $array_input[$i]['level']; $firstArr = $array_input[$i]; } echo "<pre>"; echo "OpArr:"; print_r($opArr); echo "<pre>";
Advertisement
Answer
You may give this a try. See comments for step-by-step explanation. Outputs:
array(5) { [0]=> array(2) { [0]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(1000) } [1]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(5000) } } [1]=> array(1) { [0]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(3000) } } [2]=> array(2) { [0]=> array(2) { ["level"]=> string(2) "L3" ["points"]=> int(4000) } [1]=> array(2) { ["level"]=> string(2) "L3" ["points"]=> int(6000) } } [3]=> array(3) { [0]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(4000) } [1]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(5000) } [2]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(5000) } } [4]=> array(2) { [0]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(6000) } [1]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(2000) } } }
Code:
<?php // Your input array. $array_input = [ 0 => ["level" => "L1", "points" => 1000], 1 => ["level" => "L1", "points" => 5000], 2 => ["level" =>"L2", "points" => 3000], 3 => ["level" => "L3", "points" => 4000], 4 => ["level" => "L3", "points" => 6000], 5 => ["level" => "L2", "points" => 4000], 6 => ["level" => "L2", "points" => 5000], 7 => ["level" => "L2", "points" => 5000], 8 => ["level" => "L1", "points" => 6000], 9 => ["level" => "L1", "points" => 2000] ]; $output = []; // Keep track of last group key seen so we can // create a new child array on every boundary. $lastGroupKey = $array_input[0]['level']; // Output array. $grouped = []; // Loop over every input element. foreach ($array_input as $input) { // Grouping on 'level' value, which we'll use as a 'key'. $groupKey = $input['level']; // If current key is not the last one seen, then this is a // boundary. Push the current group onto the output // and start afresh. if ($groupKey !== $lastGroupKey) { $output[] = $grouped; $grouped = []; } // Record each row. $grouped[] = $input; // Update last key seen. $lastGroupKey = $groupKey; } // The end of the array is the final boundary. // Record trailing group. $output[] = $grouped; var_dump($output); /* array(5) { [0]=> array(2) { [0]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(1000) } [1]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(5000) } } [1]=> array(1) { [0]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(3000) } } [2]=> array(2) { [0]=> array(2) { ["level"]=> string(2) "L3" ["points"]=> int(4000) } [1]=> array(2) { ["level"]=> string(2) "L3" ["points"]=> int(6000) } } [3]=> array(3) { [0]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(4000) } [1]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(5000) } [2]=> array(2) { ["level"]=> string(2) "L2" ["points"]=> int(5000) } } [4]=> array(2) { [0]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(6000) } [1]=> array(2) { ["level"]=> string(2) "L1" ["points"]=> int(2000) } } } */