I am trying to rearrange my array according to "product_type". Every 4th position of index "product_type" should be "manufacturer" in the result.
If there are too few "seller" entries then the "manufacturer" entry may be exist in the next available position (and vice versa).
My current input is like this:
$arr1 = array(
0 => ['product_type'=>'manufacturer','id'=>1],
1 => ['product_type'=>'manufacturer','id'=>2],
2 => ['product_type'=>'seller'],
3 => ['product_type'=>'seller'],
4 => ['product_type'=>'seller'],
5 => ['product_type'=>'seller'],
6 => ['product_type'=>'seller'],
7 => ['product_type'=>'manufacturer','id'=>3],
);
My expected new array should be like this:
Array(
[0] => Array
(
[product_type] => seller
)
[1] => Array
(
[product_type] => seller
)
[2] => Array
(
[product_type] => seller
)
[3] => Array
(
[product_type] => manufacturer
[id] => 1
)
[4] => Array
(
[product_type] => seller
)
[5] => Array
(
[product_type] => seller
)
[6] => Array
(
[product_type] => manufacturer
[id] => 2
)
[7] => Array
(
[product_type] => manufacturer
[id] => 3
)
)
For this solution, I am trying this, but here in the result in 1st position I get "manufacturer" product.
$arr1 = array(
0 => ['product_type'=>'manufacturer','id'=>1],
1 => ['product_type'=>'manufacturer','id'=>2],
2 => ['product_type'=>'seller'],
3 => ['product_type'=>'seller'],
4 => ['product_type'=>'seller'],
5 => ['product_type'=>'seller'],
6 => ['product_type'=>'seller'],
7 => ['product_type'=>'manufacturer','id'=>3],
) ;
function rearrange(&$arr, $n)
{
for ($i = 0; $i < $n; $i++)
{
$j = $i+1;
if($arr[$i]['product_type'] =='manufacturer'){
if($j%4!= 0){
//echo $i;
$temp = $arr[$i];
$arr[$i] = $arr[$i + 1];
$arr[$i + 1] = $temp;
} else {
if ($i==0 || $i==1 || $i==2) {
$temp = $arr[$i];
$arr[$i] = $arr[$i + 1];
$arr[$i + 1] = $temp;
} else {
$temp = $arr[$i];
$arr[$i] = $temp;
}
}
} else {
$temp = $arr[$i];
$arr[$i] = $temp;
}
}
return $arr;
}
$keys = array_keys ($arr1);
$n = count ($keys);
$arr = rearrange($arr1, $n);
print_r($arr);