php - 重新排序多维数组,以便每 4 行出现具有特定列值的行

标签 php arrays sorting for-loop multidimensional-array

我正在尝试根据“product_type”重新排列我的数组。 结果中索引“product_type”的每第 4 个位置应该是“manufacturer”。

如果“卖家”条目太少,则“制造商”条目可能存在于下一个可用位置(反之亦然)。

我目前的输入是这样的:

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

我期望的新数组应该是这样的:

 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
        )

)

对于这个解决方案,我正在尝试这个,但在第一个位置的结果中我得到了“制造商”产品。

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

FIDDLE

最佳答案

  • 在您迭代时,每隔 4 个键存储一次制造商条目。
  • 在不是每 4 个 key 的每个 key 处存储卖家条目。
  • 对新分配的键进行排序。
  • 重新索引数组。

只要您有足够的符合条件的数据,您的模式就会得到支持。当一组数据太多时,它就会在最后集中在一起。

代码:(Demo)

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

关于php - 重新排序多维数组,以便每 4 行出现具有特定列值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65103432/

相关文章:

php - 如何从 sql 数据库中获取文本字符串以在 html 代码中使用

php - 关于某些模板的数组排列

c++ - C++ 中具有十六进制值的数组

javascript - 如何根据另一个数组中的相应值对一个数组进行排序?

ios - 如何在新数组中捕获过滤后的值

php - 您如何更改您的应用程序在 heroku 平台中使用的 php 版本?

php - 如何编辑 joomla 插件?

javascript - 如何将用户的多个输入存储到localStorage?

c++ - 使用 cout 将数组和字符打印到屏幕时出现意外结果

python - 如何对列表进行排序首先写入相同的元素,然后按升序排列