合并以下数组

时间:2013-09-25 03:15:34

标签: php arrays sorting data-structures

我有以下数组:

Array
(
    [0] => Array
        (
            [Vendor_ID] => 1
            [Quantity] => 55
        )

    [1] => Array
        (
            [Vendor_ID] => 1
            [Quantity] => 55
        )

    [2] => Array
        (
            [Vendor_ID] => 1
            [Quantity] => 55
        )

    [3] => Array
        (
            [Vendor_ID] => 3
            [Quantity] => 
        )

    [4] => Array
        (
            [Vendor_ID] => 3
            [Quantity] => 
        )

    [5] => Array
        (
            [Vendor_ID] => 3
            [Quantity] => 
        )

    [6] => Array
        (
            [Vendor_ID] => 4
            [Quantity] => 
        )

    [7] => Array
        (
            [Vendor_ID] => 4
            [Quantity] => 
        )

    [8] => Array
        (
            [Vendor_ID] => 4
            [Quantity] => 
        )

)

使用以下代码创建:

$Display_Arr = array();
        $Tick = 0;
    foreach ($_POST['product'] AS $_1){
        if (!in_array($_1['vendor_id'], $Display_Arr)){
            $Display_Arr[$Tick] = array(
                "Vendor_ID" => $_1['vendor_id'],
                "Quantity" => ""
            );
            $Display_Arr[$Tick]["Quantity"] .= $_1['quantity']; 
        }else{
            $Display_Arr[$Tick]["Quantity"] .= $_1['quantity']; 
        }
        ++$Tick;
    }
    echo "<pre>";
    print_r($Display_Arr);
    echo "</pre>";

但我没有得到我想要的输出,即:

Array
(
    [0] => Array
        (
            [Vendor_ID] => 1
            [Quantity] => 55,55,55
        )


    [1] => Array
        (
            [Vendor_ID] => 3
            [Quantity] => 
        )


    [2] => Array
        (
            [Vendor_ID] => 4
            [Quantity] => 
        )

)

我在哪里错了?

@mathielo

目前的输出是:

Array
(
    [1] => Array
        (
            [Vendor_ID] => 1
            [Quantity] => 55
        )

    [3] => Array
        (
            [Vendor_ID] => 3
            [Quantity] => 
        )

    [4] => Array
        (
            [Vendor_ID] => 4
            [Quantity] => 
        )

)

然而,我正试图获得:

    [0] => Array
        (
            [Vendor_ID] => 1
            [Quantity] => 55,55,55
        )

3 个答案:

答案 0 :(得分:1)

如果我做对了,你需要的是:


编辑:这次做了一些测试并且做对了:

$Display_Arr = array();
foreach ($_POST['product'] AS $_1){
    if (!array_key_exists($_1['Vendor_ID'], $Display_Arr)){
        $Display_Arr[$_1['Vendor_ID']] = array(
            "Vendor_ID" => $_1['Vendor_ID'],
            "Quantity" => $_1['Quantity']
        );
    }else{
        if(!empty($_1['Quantity']))
            $Display_Arr[$_1['Vendor_ID']]["Quantity"] .= ",{$_1['Quantity']}"; 
    }

}
echo "<pre>";
print_r($Display_Arr);
echo "</pre>";

主要问题出在if (!in_array($_1['vendor_id'], $Display_Arr))。 PHP的in_array()检查数组中给定的needle,我们尝试匹配外部数组键中存储的Vendor_ID值< / em>的。这是使用array_key_exists()修复的。

编辑2:我将此用于测试数据:

$_POST['product'] = array(
    0 => array(
        'Vendor_ID' => 1,
        'Quantity' => 55
    ),
    1 => array(
        'Vendor_ID' => 1,
        'Quantity' => 55
    ),
    2 => array(
        'Vendor_ID' => 1,
        'Quantity' => 55
    )
    ,
    3 => array(
        'Vendor_ID' => 3,
        'Quantity' => ''
    ),
    4 => array(
        'Vendor_ID' => 3,
        'Quantity' => ''
    ),
    5 => array(
        'Vendor_ID' => 3,
        'Quantity' => ''
    ),
    6 => array(
        'Vendor_ID' => 4,
        'Quantity' => ''
    ),
    7 => array(
        'Vendor_ID' => 4,
        'Quantity' => ''
    ),
    8 => array(
        'Vendor_ID' => 4,
        'Quantity' => ''
    )
);

您不再需要$Tick,因为您可以使用Vendor_ID作为外部数组的键。

答案 1 :(得分:1)

看起来解决此问题的最简单方法是首先聚合供应商数量,然后使用您正在使用的密钥构建最终数组。

我假设输入看起来像这样:

$_POST['product'] = [
    ['vendor_id' => 1, 'quantity' => 55],
    ['vendor_id' => 1, 'quantity' => 55],
    ['vendor_id' => 1, 'quantity' => 55],
    ['vendor_id' => 3, 'quantity' => null],
    ['vendor_id' => 3, 'quantity' => null],
    ['vendor_id' => 3, 'quantity' => null],
    ['vendor_id' => 4, 'quantity' => null],
    ['vendor_id' => 4, 'quantity' => null],
    ['vendor_id' => 4, 'quantity' => null],
];

汇总数量:

$aggregates = [];
foreach ($_POST['product'] as $product) {
    $id = $product['vendor_id'];
    if ( ! isset($aggregates[$id])) {
        $aggregates[$id] = [];
    }
    if ($product['quantity'] > 0) {
        $aggregates[$id][] = $product['quantity'];
    }
}

聚合数组现在应如下所示:

$aggregates = [
    1 => [
        0 => 55,
        1 => 55,
        2 => 55,
    ],
    3 => [], // Empty
    4 => [], // Empty
];

正如您所看到的,数据现在整齐有序,可以随心所欲地放入任何格式。使用您在问题中使用的键就可以了:

$output = [];
foreach ($aggregates as $vid => $qty) {
    $quantity = implode(',', $qty);
    $output[] = ['Vendor_ID' => $vid, 'Quantity' => $quantity];
}

输出现在应如下所示:

$output = [
    ['Vendor_ID' => 1, 'Quantity' => '55,55,55'],
    ['Vendor_ID' => 3, 'Quantity' => ''],
    ['Vendor_ID' => 4, 'Quantity' => ''],
];

答案 2 :(得分:0)

虽然最后一个答案的输出(Sverri M. Olsen)更有用,但这将输出您正在寻找的内容。在这里,您将数量作为字符串获得,而使用Sverri的方法,您首先得到一个数组。

$Display_Arr = array();
$vendors=array();
foreach ($_POST['product'] AS $_1){

  if (!in_array($_1['vendor_id'],$vendors)){
    $vendors[]=$_1['vendor_id'];
    $Display_Arr[sizeof($vendors)-1] = array(
      "Vendor_ID" => $_1['vendor_id'],
      "Quantity" => $_1['quantity']
    );
  }
  else{
    $vendorKey=array_search($_1['vendor_id'],$vendors);
    $Display_Arr[$vendorKey]["Quantity"] .=(!empty($Display_Arr[$vendorKey]["Quantity"])?',':null).$_1['quantity'];
  }
}