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

3

请帮忙!我想验证具有重复子数组值的数组。我有一个多维数组。我想返回具有重复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)

1
整洁,现在尝试编写一些代码。 - user6763587
我已经更新了我的答案,现在它可以为多个重复对提供所要求的精确输出。你能否看一下它并看看它是否解决了你的问题? - glaux
2个回答

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
使用此代码获取重复产品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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接