0

我有以下数组:

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
        )
4

3 回答 3

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)). PHPin_array()检查needle数组values中的给定值,我们试图匹配Vendor_ID存储在外部数组keys中的值。这是使用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其用作外部数组的键。

于 2013-09-25T03:22:18.397 回答
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' => ''],
];
于 2013-10-17T00:46:53.743 回答
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'];
  }
}
于 2013-10-17T14:14:11.387 回答