检查是否存在数组中每个键的所有值?

时间:2017-05-12 09:07:57

标签: php

我有一个像这样的$ arrays数组:

Array
(
[0] => Array
    (
        [0] => VUM
        [1] => UA0885
    )

[1] => Array
    (
        [0] => VUA
        [1] => UA0885
    )

我想检查输入值是否存在(VUA& UA0885),而不是将其添加到此数组中。 例如:

(VUA & UA0885) => not add
(VUB & UA0885) => add
(VUA & UA0886) => add

这是我的代码:

foreach($arrays as $array){
    if($array[0] != $_REQUEST['tourcode'] || $array[1] != $_REQUEST['promocode']){
    $arrays[] = array($_REQUEST['tourcode'],$_REQUEST['promocode']);
    }

}

尝试使用in_array但它仍然向$ arrays添加重复

4 个答案:

答案 0 :(得分:1)

您可以迭代数组,检查是否找到相同的值,如果没有推送新值:

$tour  = $_REQUEST['tourcode'];
$promo = $_REQUEST['promocode'];

$new = true; //default to true
foreach($arrays as $el){
    if($el[0].'-'.$el[1] == $tour. '-' .$promo]){
        $new=false;
        break; //no need to continue
    }
}

if($new) $arrays[]=[$tour,$promo];

答案 1 :(得分:0)

foreach($arrays as $key => $array) {
    if($array[0] == $_REQUEST['tourcode'] && $array[1] == $_REQUEST['promocode']) {
        unset($arrays[$key]);
    }
}

答案 2 :(得分:0)

据我所知,改变你的声明!in_array可能是解决方案:

if (!in_array(array($_REQUEST['tourcode'],$_REQUEST['promocode']),$array))

答案 3 :(得分:0)

<?php 
$array = array(
  0=>array(
     0=>'VUM',
     1=>'UA0885'
  ),

  1=>array(
    0=>'VUA',
    1=>'UA0885'
  )
);

$tour_code = trim($_REQUEST['tourcode']);
$promo_code = trim($_REQUEST['promocode']); 

$filterarray = array();
$counter = 0;
foreach($array as $subarray){
    foreach($subarray as $key => $value){
        if(!in_array($tour_code , $subarray) || !in_array($promo_code , $subarray)){
            $filterarray[$counter][] = $value;
        }
    }
    $counter++;    
}
print_r($filterarray);
?>