如何根据PHP中的索引值将2D数组拆分为数组块

时间:2014-12-22 06:58:37

标签: php arrays

我有一个2D数组,其中包含产品ID,产品名称和公司ID,现在我想制作有产品信息的公司组,并且可以通过产品详细信息发送给公司,我有数据如

Array
(
[0] => Array
    (
        [id] => 5363
        [proname] => pro1
        [company_id] => 101
    )

[1] => Array
    (
        [id] => 5364
        [proname] => pro2
        [company_id] => 105
    )

[2] => Array
    (
        [id] => 5366
        [proname] => pro3
        [company_id] => 102
    )

[3] => Array
    (
        [id] => 5367
        [proname] => pro4
        [company_id] => 101
    )

[4] => Array
    (
        [id] => 5368
        [proname] => pro5
        [company_id] => 105
    )

[5] => Array
    (
        [id] => 5369
        [proname] => pro6
        [company_id] => 105
    )

[6] => Array
    (
        [id] => 3847
        [proname] => pro7
        [company_id] => 102
    )

[7] => Array
    (
        [id] => 3849
        [proname] => pro8
        [company_id] => 105
    )

[8] => Array
    (
        [id] => 5371
        [proname] => pro9
        [company_id] => 101
    )

[9] => Array
    (
        [id] => 5383
        [proname] => pro10
        [company_id] => 102
    )

[10] => Array
    (
        [id] => 5385
        [proname] => pro11
        [company_id] => 105
    )

)

我想在company_id的基础上创建数组块,因此最终的数组应该是3-d数组,输出应该是这样的

array(

   [0]=>Array(
        [0] => Array
            (
                [id] => 5363
                [proname] => pro1
                [company_id] => 101
            )

        [1] => Array
            (
                [id] => 5367
                [proname] => pro4
                [company_id] => 101
            )
        [2] => Array
            (
                [id] => 5371
                [proname] => pro9
                [company_id] => 101
            )

        )
    [1]=Array
        (
        [0] => Array
            (
                [id] => 5364
                [proname] => pro2
                [company_id] => 105
            )
        [1] => Array
            (
                [id] => 5368
                [proname] => pro5
                [company_id] => 105
            )
        [2] => Array
            (
                [id] => 5369
                [proname] => pro6
                [company_id] => 105
            )
        [3] => Array
            (
                [id] => 3849
                [proname] => pro8
                [company_id] => 105
            )
         [4] => Array
            (
                [id] => 5385
                [proname] => pro11
                [company_id] => 105
            )
        )

    [2]=Array(
         [0] => Array
            (
                [id] => 5366
                [proname] => pro3
                [company_id] => 102
            )
          [1] => Array
            (
                [id] => 3847
                [proname] => pro7
                [company_id] => 102
            )
        [2] => Array
            (
                [id] => 5383
                [proname] => pro10
                [company_id] => 102
            )
    )

)

目标是使团队成为公司。

3 个答案:

答案 0 :(得分:3)

试试这个:

最简单的方法:

  $newarray = array();
    foreach($array as $values) {
        $newarray[$values['company_id']][] = $values;
    }
    $newarray = array_values($newarray); 
另一种方式:

<?php 
$newarray = array();

foreach($array as $key=>$val){// here $array is your given array.
if(isset($newarray[$val['company_id']])){
    array_push($newarray[$val['company_id']],$val);

}else{

    $newarray[$val['company_id']][0]=$val;
}

}
$newarray = array_values($newarray);
echo "<pre>";
print_r($newarray);
echo "</pre>";
?>

答案 1 :(得分:0)

循环遍历数组。

在这里,您将获得由第三级(我们期望的数组)组成的第二维数组。

检查密钥是否为company_id并附加到我们在循环之前创建的新数组。

$newArr = array();
foreach ($arr as $array) {
  foreach ($array as $k=>$v) {
    if ($k == 'company_id')  {
      $newArr[$k][] = $array;
    }
  }
}
print_r($newArr); // Your new array()

答案 2 :(得分:0)

$groupBycompany = [];    
if ($companies) {
          foreach ($companies as $keys => $values) {
            if (isset($values['company_id'])){
                   $groupByCompany[$values['company_id']][] = $values;
            }
          }
    }