<?php
$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],
);
$manuCounter = 0;
$sellerCounter = 0;
$result = [];
foreach ($arr1 as $row) {
if ($row['product_type'] === 'manufacturer') {
$manuCounter += 4;
$result[$manuCounter] = $row;
} else {
++$sellerCounter;
if (!($sellerCounter % 4)) {
++$sellerCounter;
}
$result[$sellerCounter] = $row;
}
}
ksort($result);
var_export(array_values($result));
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
- 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,
),
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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,
),
)
preferences:
148.62 ms | 403 KiB | 150 Q