Is there a php code that can move a multidimensional array minimum of 5 iterations if the values has a duplicate?
This is an example code.
<?php
$results = array(
array('value' => 10),
array('value' => 2),
array('value' => 1),
array('value' => 3),
array('value' => 2), //This will move
array('value' => 4),
array('value' => 5),
array('value' => 5), //This will move
array('value' => 3), //This will move
array('value' => 4), //This will move
array('value' => 10), //Ok reach minimum of 5 count
array('value' => 9),
array('value' => 8),
array('value' => 7),
array('value' => 7), // This will move
array('value' => 8), // This will move
array('value' => 1), //Ok reach minimum of 5 count
array('value' => 6),
array('value' => 6), // This will move
array('value' => 19) //Ok reach minimum of 5 count
);
);
?>
This is the basic idea. I want to move the values if they found a duplicate within a minimum of 5 iterations. The moving of iterations can move greater than 5. The data can be random. It will loop to find a better result. Is this possible?
Here is my expected results. This can be other results that can satisfies the logic.
<?php
$results = array(
array(
array('value' => 6), //Ok
array('value' => 9), //Ok
array('value' => 2), //Ok
array('value' => 7), //Ok
array('value' => 1), //Ok
array('value' => 4), //Ok
array('value' => 8), //Ok
array('value' => 5), //Ok
array('value' => 9), //Ok
array('value' => 2), //Ok
array('value' => 3), //Ok
array('value' => 6), //Ok
array('value' => 4), //Ok
array('value' => 10), //Ok
array('value' => 8), //Ok
array('value' => 7), //Ok
array('value' => 1), //Ok
array('value' => 3), //Ok
array('value' => 5), //Ok
array('value' => 10) //Ok
);
);
?>
Hope you can help me.
EDIT:
Here is my code
<?php
$required_array = array();
$temp = "";
$temp2 = "";
foreach($array as $key=>$child) {
if( $child["value"]==$temp ) {
$i = $key+5;
$required_array[$i] = $child;
} else {
$i = $key;
//if(isset($required_array[$i])) $i++;
while(isset($required_array[$i])) {
$i++;
}
$required_array[$i] = $child;
}
$temp = $child["value"];
}
ksort($required_array);
print_r($required_array);
//Tried this but always move on five iterations and found duplicate within the range of five
array_chunk solve my issue.
<?php
$chunks = array_chunk($array, ceil(count($array)/5));
$array = array();
for($x = 0, $numX = count($chunks[0]); $x < $numX; $x++){
for($y = 0, $numY = count($chunks); $y < $numY; $y++){
if(isset($chunks[$y][$x]))
//echo $x.' '.$y.'<br>';
$array[] = $chunks[$y][$x];
}
}
print_r($array);
?><br>
Anyway, my next problem is if I have two rows in an array. Like the example below.
<?php
$array = array(
array('value' => 1, 'value2' => 2),
array('value' => 3, 'value2' => 1)
);
?><br>
How can I check both sides (value and value2) for duplicates and used my existing code?