【问题标题】:how to merge array indexes having same value in php [closed]如何在php中合并具有相同值的数组索引[关闭]
【发布时间】:2016-06-22 07:53:48
【问题描述】:

我有一个数组,在每个索引上都有这样的值我有一个嵌套数组有[cats][products]

   Array
(
    [0] => Array
        (
            [cat] => 6,
            [products] => 3
        ),

    [1] => Array
        (
            [cat] => 6,
            [products] => 4
        ),
   [2] => Array
        (
            [cat] => 7,
            [products] => 9
        )
)

我想更改这个数组,就像所有具有相同值的 [cats] 索引合并到单个索引中一样,这些索引中的 [products]index 合并到嵌套数组中

  Array
(
    [0] => Array
        (
            [cat] => 6
            [products] => array(
                                [0]=>3,
                                [1]=>4 )
        ),

    [1] => Array
        (
            [cat] => 7,
            [products] => 9
        )
  )

我试过了,但我的代码不起作用我的代码是这样的

$products = array(
    array(
        'cat' => 6,
        'products' => 3
    ),
    array(
        'cat' => 6,
        'products' => 4
    ),
    array(
        'cat' => 7,
        'products' => 9
    )
);
$newProducts = array();
foreach ($products as $item) {
    if (!empty($newProducts[$item['cat']])) {
        $currentValue = $newProducts[$item['cat']]['products'];
        $newProducts[$item['cat']]['products'] = $item['products'];
    } else {
        $newProducts[$item['cat']] = $item;
    }
}

【问题讨论】:

  • 到目前为止您尝试过什么?发布您迄今为止尝试过的尝试。
  • 其实我很困惑我不知道该怎么做..
  • 您可能已经浏览了一些搜索教程或代码,您可能已经尝试过一些东西,然后您可能已经在 SO 上发布了问题。或者你到目前为止还没有尝试过,直接在 SO 上发布
  • 从写foreach开始
  • 遍历数组并生成一个包含子列表的新数组。我建议始终使用[products] 下的子数组,即使它只包含一个条目。这使得创建和以后使用它变得更容易(您不必为一种产品和多种产品处理不同的情况)。

标签: php arrays foreach merge


【解决方案1】:

这个脚本可能会有所帮助。

忽略 $arr1,$arr2,$arr3。我刚刚创建它们是为了制作一个像你一样的数组。

$arr1 = array(
    'cat' => 6,
    'products' => 3,
);
$arr2 = array(
    'cat' => 6,
    'products' => 4,
);
$arr3 = array(
    'cat' => 7,
    'products' => 9,
);
$arr4 = array($arr1, $arr2, $arr3);

print_r($arr4);

$n = count($arr4);

for($i=0; $i<$n; $i++){
    if($arr4[$i]['cat'] == $arr4[$i+1]['cat']){
        $arr5[$i]['cat'] = $arr4[$i]['cat'];
        $arr5[$i]['products'] = array($arr4[$i]['products'],$arr4[$i+1]['products']);
    }
    else {
        $arr5[] = $arr4[$i+1];
    }
}
array_pop($arr5); // just to delete the last null index of the array,
                   //created by the loop.
print_r($arr5)

输出-

   Array
(
    [0] => Array
        (
            [cat] => 6
            [products] => Array
                (
                    [0] => 3
                    [1] => 4
                )

        )

    [1] => Array
        (
            [cat] => 7
            [products] => 9
        )

)

【讨论】:

    【解决方案2】:

    试试这个:

    <?php
    
    $array = array(
        array(
            'cat'=>6,
            'product'=>3,
        ),
        array(
            'cat'=>6,
            'product'=>4,
        ),
        array(
            'cat'=>5,
            'product'=>9,
        ),
    );
    $catIndex = array();
    
    foreach($array as $subarray)
    {
        if(!isset($catIndex[$subarray['cat']]))
            $catIndex[$subarray['cat']] = array('cat'=>$subarray['cat'], 'products'=>array());
    
        $catIndex[$subarray['cat']]['products'][] = $subarray['product'];
    
    }
    
    var_dump(array_values($catIndex));
    

    工作示例:CLICK!

    【讨论】:

    • 感谢您的帮助
    【解决方案3】:

    正如其他人所说,您应该从foreach开始

    <?php
    
    $products = array(
        array(
            'cat' => 6,
            'products' => 3
        ),
        array(
            'cat' => 6,
            'products' => 4
        ),
        array(
            'cat' => 7,
            'products' => 9
        )
    );
    $newProducts = array();
    foreach ($products as $item) {
        if (!empty($newProducts[$item['cat']])) {
            $currentValue = (array) $newProducts[$item['cat']]['products'];
            $newProducts[$item['cat']]['products'] = array_merge($currentValue, (array) $item['products']);
        } else {
            $newProducts[$item['cat']] = $item;
        }
    }
    var_dump(array_values($newProducts));
    

    输出如下:

    array(2) {
      [0]=>
      array(2) {
        ["cat"]=>
        int(6)
        ["products"]=>
        array(2) {
          [0]=>
          int(3)
          [1]=>
          int(4)
        }
      }
      [1]=>
      array(2) {
        ["cat"]=>
        int(7)
        ["products"]=>
        int(9)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      相关资源
      最近更新 更多