检查数组

时间:2017-09-27 14:58:10

标签: php arrays

我试图比较一个数组中的索引,如果所有索引都不相同那么它应该发送错误。如果它们相同,则将一个值作为字符串。

首先我从db中检索数据,

$idNaloga = array();

 while($r=$ss->fetch()){
   $idNaloga[] = $r['idNaloga'];
 }

如果我收到这样的数组,它应该显示错误,因为72

array(3) {
  [0]=>
  string(2) "19"
  [1]=>
  string(2) "19"
  [2]=>
  string(2) "72"
}

如果是结果,则应将19作为字符串。

array(4) {
  [0]=>
  string(2) "19"
  [1]=>
  string(2) "19"
  [2]=>
  string(2) "19"
  [3]=>
  string(2) "19"
}

我试图使用array_unique()函数,但无法获得正确的结果。我在谷歌搜索但找不到它。

4 个答案:

答案 0 :(得分:3)

是的,array_unique应该有效。算一下它的结果吧。如果它有多个条目,则显示错误。

if (count(array_unique($your_array)) > 1) {
    echo  'error: multiple values';
};

答案 1 :(得分:1)

您可以使用array_unique

执行此操作
if (count(array_unique($idNaloga)) > 1) {
     //return error
} else {
    echo $idNaloga[0];
}

答案 2 :(得分:0)

假设我正确地解释了你的数组,你可以创建一个只计算唯一值的数量和错误或返回唯一值的函数。

$a = array( 0 => "19", 1 => "19", 2 => "72" );

$b = array(0 => "19", 1 => "19",2 => "19", 3 => "19");

function checkUniqueValues($arr) {
    $vars = array_unique($arr);
    if (count($vars) > 1) {
         echo "error!";   
    } else {
         // use a return if you want
         echo $vars[0];
    }
}

checkUniqueValues($a);
echo "<br />";
checkUniqueValues($b);

答案 3 :(得分:0)

以下是在php小提琴网站上测试的不同案例。您可以选择符合您要求的代码。

<?php

$a = ["19", "19", "19" ];

$b = ["19", "19", "19" ];

$c = ["19", "19", "19", "19"];

$d = ["19","19", "72"];
//method 1
if($a == $b){
    echo "Array a and b are equal";
    echo "<br>";
}else{
     echo "Array a and b are not equal";
    echo "<br>";
}

if($a == $c){
    echo "Array a and c are equal";
    echo "<br>";
}else{
     echo "Array a and c are not equal";
    echo "<br>";
}

if($a == $d){
    echo "Array a and d are equal";
    echo "<br>";
}else{
     echo "Array a and d are not equal";
    echo "<br>";
}

//method 2
array_multisort($a);
    array_multisort($b);
array_multisort($c);
    array_multisort($d);
if(serialize($a) === serialize($b)){
    echo "key matches with values for b";
     echo "<br>";
}
else{
 echo "key doesnt matches with values for b";
     echo "<br>";
}
if(serialize($a) === serialize($c)){
    echo "key matches with values  for c";
     echo "<br>";
}
else{
 echo "key doesnt matches with values for c";
     echo "<br>";
}
if(serialize($a) === serialize($d)){
    echo "key matches with values  for d";
     echo "<br>";
}
else{
 echo "key doesnt matches with values for d";
     echo "<br>";
}


//method 3 (if count doesnt match)
echo "<br>";
$max_count_between_a_and_c = count($a)>count($c)?count($a):count($c);

$error = false;
for($i = 0; $i < $max_count_between_a_and_c; $i++){
    if(isset($a[$i]) && isset($c[$i]) && $a[$i] != $c[$i]) 
        $error =  true;
}
if($error)
       echo "array a and array c are not equal";
else
       echo "array a and array c are equal";

echo "<br>";

$max_count_between_a_and_d = count($a)>count($d)?count($a):count($d);
$error = false;
for($i = 0; $i < $max_count_between_a_and_d; $i++){
    if(isset($a[$i]) && isset($d[$i]) && $a[$i] != $d[$i])
        $error =  true;
}
if($error)
       echo "array a and array d are not equal";
else
       echo "array a and array d are equal";





?>

<强>输出

Array a and b are equal
Array a and c are not equal
Array a and d are not equal
key matches with values for b
key doesnt matches with values for c
key doesnt matches with values for d

array a and array c are equal
array a and array d are not equal