具有重复值的多维数组返回键

时间:2017-04-03 03:38:04

标签: php arrays laravel-5.2

请帮忙!我想验证具有重复子数组值的数组。我有一个多维数组。我想返回具有重复product_id值的子数组的键。示例:在我的数组中,我有子数组,其中包含重复的product_id = 124.我想返回它们的密钥。

[purchase_order_products] => Array
    (
        [0] => Array
            (
                [product_id] => 124
                [barcode] => 480001407081
                [item_code] => 
                [name] => Brew Kettle Can 330mL
                [qty] => 
                [unit] => 2
                [pcs_have] => 1
                [total_pcs] => 1
                [cost] => 34.83
                [total_item_price] => 34.83
                [stocks] => 
                [po_qty] => 
            )

        [1] => Array
            (
                [product_id] => 125
                [barcode] => 480001416108
                [item_code] => 
                [name] => Colt 45 Can 330mL
                [qty] => 
                [unit] => 2
                [pcs_have] => 1
                [total_pcs] => 1
                [cost] => 29.58
                [total_item_price] => 29.58
                [stocks] => 
                [po_qty] => 
            )

        [2] => Array
            (
                [product_id] => 124
                [barcode] => 480001407081
                [item_code] => 
                [name] => Brew Kettle Can 330mL
                [qty] => 
                [unit] => 2
                [pcs_have] => 1
                [total_pcs] => 1
                [cost] => 34.83
                [total_item_price] => 34.83
                [stocks] => 
                [po_qty] => 
            )
)

我想要的输出是:

Array(0,2)

2 个答案:

答案 0 :(得分:1)

编辑:我已经相当多地更新了答案 编辑2:现在利用内置数组函数来查找重复项

$products = [
  0 => ['product-id' => 124],
  1 => ['product-id' => 125],
  2 => ['product-id' => 124],
  3 => ['product-id' => 126],
  4 => ['product-id' => 126],
  8 => ['product-id' => 124],
];

// Find the duplicates
$product_ids = array_column($products, 'product-id');
$count = array_count_values($product_ids);
$duplicates = array_filter($count, function($var) {
  return $var > 1;
});

// List all the entries with duplicate ids:
foreach ( array_flip($duplicates) as $product_id ) {
  $filter = array_filter($products, function($var) use ($product_id) {
    return ( $var['product-id'] === $product_id );
  });
  print_r('Product-id: ' . $product_id . ' is duplicated in entries: ');
  print_r(array_keys($filter));
}

输出:

// Product-id: 124 is duplicated in entries: Array
// (
//     [0] => 0
//     [1] => 2
//     [2] => 8
// )
// Product-id: 126 is duplicated in entries: Array
// (
//     [0] => 3
//     [1] => 4
// )

答案 1 :(得分:1)

使用此代码获取重复产品ID的密钥:

$products = $array['purchase_order_products'];

$duplicate_products_keys = array();
$products_ids = array();

foreach($products as $key => $product) {
   if(in_array($product['product_id'], $products_ids)) {
      $duplicate_products_keys[] = $key;
   }
   $products_ids[$product['product_id']] = $product['product_id'];
}

prinr_r($duplicate_products_keys);