将数组中的类似值分组

时间:2017-08-02 13:02:05

标签: php ajax

这是我通过Ajax发布并进入ajax.php文件的数组。现在我想添加一个新数组,如下所示。

Array
(
    [0] => Array
        (
            [contact_id] => 5000
            [invoice] => 476
        )

    [1] => Array
        (
            [contact_id] => 5000
            [invoice] => 396
        )

    [2] => Array
        (
            [contact_id] => 1490
            [invoice] => 1083
        )

    [3] => Array
        (
            [contact_id] => 1490
            [invoice] => 498


  )
)

如果联系人ID相同,我也想添加此项,然后会添加发票

Array
(
    [0] => Array
        (
            [contact_id] => 5000
            [invoice] => Array (
                    [0] =>476,
                    [1] =>396
            )
        )

    [2] => Array
        (
            [contact_id] => 1490
            [invoice] => Array (
                    [0] =>1083,
                    [1] =>498
            )
        )
)

我试图这样做,没有成功。这是我的代码:

$invtemp =array();
 foreach($_POST['invoice_id'] as $value){
    if(!in_array($value['contact_id'], $invtemp, true)){
         $arr = array($value['contact_id'] => $value['invoice'] );
         array_push($invtemp, $arr);    
    }
}

3 个答案:

答案 0 :(得分:1)

我认为你可以用简单的foreach来实现:

foreach ($array as $value) {
    $arr[$value['contact_id']]['contact_id'] = $value['contact_id'];
    $arr[$value['contact_id']]['invoice'][] = $value['invoice'];
}

print_r($arr);

这提供:

Array
(
    [5000] => Array
        (
            [contact_id] => 5000
            [invoice] => Array
                (
                    [0] => 476
                    [1] => 396
                )

        )

    [1490] => Array
        (
            [contact_id] => 1490
            [invoice] => Array
                (
                    [0] => 1083
                    [1] => 498
                )

        )

)

要重置密钥:

print_r(array_values($arr));

不那么重要,但为了使其更清洁,您可以使用extract

foreach ($array as $value) {
    extract($value);
    $arr[$contact_id]['contact_id'] = $contact_id;
    $arr[$contact_id]['invoice'][] = $invoice;
}

print_r($arr);

答案 1 :(得分:0)

简单明了。

$invtemp = array();
foreach($_POST['invoice_id'] as $value){
    $invtemp[$value['contact_id']]['invoice'][] = $value['invoice'];
}

通过contact_id索引数组,可以确保每个contact_id只有一个子数组。

通过将每张新发票添加为新索引,即使已经有与contact_id关联的发票,您也可以确保将每张发票添加到最终数组中。

由于您的密钥是contact_ids,因此您无需将它们存储为值以使其可用。

答案 2 :(得分:0)

这将创建一个类似于您想要的数组,但密钥为contact_id 它创建了一个稍微紧凑的阵列,仍然易于使用。

$arr = array
(
Array
    (
        "contact_id" => 5000,
        'invoice' => 476
    ),

Array
    (
        'contact_id' => 5000,
        'invoice' => 396
    ),

Array
    (
        'contact_id' => 1490,
        'invoice' => 1083
    ),

Array
    (
        'contact_id' => 1490,
        'invoice' => 498


));

$res =array();
Foreach($arr as $subarr){
    Foreach($subarr as $key => $val){
       If($key == "contact_id") {
          If(!isset($res[$val])) $res[$val] = array();
          $cid = $val;
       }Else{
          $res[$cid][]= $val;
       }
    }
}

Var_dump($res);

输出:

array(2) {
 [5000]=>
array(2) {
    [0]=> int(476)
    [1]=> int(396)
  }
[1490]=> 
array(2) {
    [0]=> int(1083)
    [1]=> int(498)
  }
}

见这里:https://3v4l.org/cmH8P