如何检查数组的键值以防止在php中重复输入?

问题描述:

I know this is a very common and repeated subject, but after spending some hours of googling I didn't find the right solution for my problem.

Using a foreach loop I'm filling an array, each row including a key and value. I just want to prevent duplicates or remove them.

$data['products'] = array();
$data['stores'] = array();

foreach ($products as $product) {
    //if (!in_array($product['store_id'], $data['stores'])) {
    if (!array_key_exists($product['store_id'], $data['stores'])) {
        $data['stores'][] = array(
            'store_id'   => $product['store_id'],
            'store_name' => $product['store_name']
        );
    }
}

The result of print_r($data['stores']):

Array
(
    [0] => Array
        (
            [store_id] => 1
            [store_name] => Karaca
        )

    [1] => Array
        (
            [store_id] => 1
            [store_name] => Karaca
        )

    [2] => Array
        (
            [store_id] => 7
            [store_name] => nurteks
        )

    [3] => Array
        (
            [store_id] => 7
            [store_name] => nurteks
        )
)

I tried all suggestions, but I don't know how to figure it out.
Thanks for any kind help!

我知道这是一个非常常见和重复的主题,但在花了几个小时的谷歌搜索后我没有找到 正确解决我的问题。 p>

使用 foreach code>循环我正在填充数组,每行包括一个键和值。 我只是想阻止 duplicates code>或删除它们。 p>

  $ data ['products'] = array(); 
 $ data ['stores'] = array(); 
 
foreach($ products as $ product  ){
 // if(!in_array($ product ['store_id'],$ data ['stores'])){
 if(!array_key_exists($ product ['store_id'],$ data ['stores'  ])){
 $ data ['stores'] [] = array(
'store_id'=> $ product ['store_id'],
'store_name'=> $ product ['store_name'] \  n); 
} 
} 
  code>  pre> 
 
 

print_r($ data ['stores']) code>的结果: p >

 数组
(
 [0] =>数组
(
 [store_id] => 1 
 [store_name] => Karaca 
)
  
 [1] =>数组
(
 [store_id] => 1 
 [store_name] => Karaca 
)
 
 [2] =>数组
(
 [  store_id] => 7 
 [store_name] => nurteks 
)
 
 [3] =>数组
(
 [store_id] => 7 
 [store_name] => nurteks  
)
)
  code>  pre> 
 
  

我尝试了所有建议,但我不知道如何解决这个问题。
谢谢你的帮助! p> div>

Use the store ID as the key in the stores array. This will ensure that the entries are unique since array keys are inherently unique.

foreach ($products as $product) {
    $data['stores'][$product['store_id']] = array(
        'store_id'   => $product['store_id'],
        'store_name' => $product['store_name']
    );
}

The two things you tried didn't work because:

  1. if (!in_array($product['store_id'], $data['stores'])) {...
    The "needle" ($product['store_id']) is an integer, but the "haystack" ($data['stores']) contains arrays.

  2. if (!array_key_exists($product['store_id'], $data['stores'])) {
    You aren't specifying array keys when you use $data['stores'][] to append items, so the store id won't be found as an array key, or if it is, it won't really be the store id, it will just be the automatically assigned sequential index that just coincidentally matches a store id.

Another solution could be saving the store Ids and test if it's exist or not

$storeIds = [];
foreach ($products as $product) {
    if(!empty( $storeIds[$product['store_id']])){
        //the store is taken before
    }else{
        // new store
        $data['stores'][] = array(
            'store_id'   => $product['store_id'],
            'store_name' => $product['store_name']
        );
    }
    $storeIds[$product['store_id']] = true;//save the store id

}