PHP - 如何检查数组中是否已存在值?

时间:2016-09-25 13:54:32

标签: php arrays

以下是我制作数组的方法:

if($Love != " "){
    $LoveArray['Couple'] = $Love;
}

上面的行是while循环。当我print_r($LoveArray);时,我收到以下输出:

  

数组([Couple] => 5726)数组([Couple] => 3421)数组([Couple]   => 3421)阵列([Couple] => 3421)阵列([Couple] => 2213)阵列([Couple] => 2213)阵列([Couple] => 5339)阵列([Couple] =>   5339)数组([Couple] => 3910)数组([Couple] => 3910)数组(   [夫妻] => 3910)阵列([Couple] => 3910)阵列([Couple] => 3910   )Array([Couple] => 3910)Array([Couple] => 3910)Array(   [夫妻] => 3910)阵列([Couple] => 3421)阵列([Couple] => 5726   )Array([Couple] => 2213)Array([Couple] => 2213)Array(   [夫妻] => 2213)阵列([Couple] => 2213)阵列([Couple] => 2213   )Array([Couple] => 2213)Array([Couple] => 2213)Array(   [夫妻] => 2213)阵列([Couple] => 2213)阵列([Couple] => 2213   )Array([Couple] => 2213)Array([Couple] => 2213)Array(   [夫妻] => 6327)阵列([Couple] => 6327)阵列([Couple] => 3910   )Array([Couple] => 3910)Array([Couple] => 3910)Array(   [夫妻] => 3910)阵列([Couple] => 3910)阵列([Couple] => 3910   )Array([Couple] => 3910)Array([Couple] => 3910)Array(   [夫妻] => 3910)阵列([Couple] => 5339)阵列([Couple] => 5339   )Array([Couple] => 6327)Array([Couple] => 6327)Array(   [夫妻] => 6327)阵列([Couple] => 6327)阵列([Couple] => 6327   )

上面的数组可以表示为:

<?php

    $array  = [
        ["Couple" => 5726],

        ["Couple" => 3421],
        ["Couple" => 3421],
        ["Couple" => 3421],

        ["Couple" => 2213],
        ["Couple" => 2213],

        ["Couple" => 5339],
        ["Couple" => 5339],

        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],

        ["Couple" => 3910],
        ["Couple" => 3421],
        ["Couple" => 5726],

        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],
        ["Couple" => 2213],

        ["Couple" => 6327],
        ["Couple" => 6327],

        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],
        ["Couple" => 3910],

        ["Couple" => 5339],
        ["Couple" => 5339],

        ["Couple" => 6327],
        ["Couple" => 6327],
        ["Couple" => 6327],
        ["Couple" => 6327],
        ["Couple" => 6327],
    ];

正如您所看到的,有些值正在重复,其中一个是5726

我如何检查5726中是否已$LoveEvent['Couple'],以及是否在此数组中不插入另一个?{/ p>

亲切的问候。

4 个答案:

答案 0 :(得分:0)

如果需要检查数组中是否存在值,则首选使用in_array()。

但是到目前为止,您需要检查它是否为多维数组,以便您可以以递归方式使用in_array(),以便它可以用于查找值是否存在于数组中。

<?php
function check_array_value($to_find, $array_values, $strict = false) {
    foreach ($array_values as $item) {
        if (($strict ? $item === $to_find : $item == $to_find) || (is_array($item) && check_array_value($to_find, $item, $strict))) {
            return true;
        }
    }
    return false;
}
?>

可以像这样使用此功能。

$array_values = array(array("Name", "Apple"), array("Orange", "IOS"));
echo check_array_value("IOS", $array_values) ? 'found' : 'not found';

如果数组中存在IOS,它将返回found,否则它将返回not found

答案 1 :(得分:0)

可能是这样的:

foreach($LoveArray as $value){
    if($value['Couple'] != $Love){
        $LoveArray['Couple'] = $Love;
    }
}

答案 2 :(得分:0)

if ($Love != " ") {
    if (!in_array($Love, array_column($LoveArray, 'Couple'))) {
        $LoveArray['Couple'] = $Love;
        $finalArray[] = $LoveArray; //adding the new element should also be here
    }
}

需要 (PHP 5&gt; = 5.5.0,PHP 7) http://php.net/manual/en/function.array-column.php

$arr = ['orange', 'apple', 'banana', 'orange', 'banana'];
$finalArray = [];

foreach ($arr as $item) {
    if (!in_array($item, array_column($finalArray, 'Couple'))) {
        $finalArray[] = ['Couple' => $item];
    }
}

print_r($finalArray);

输出:

Array
(
    [0] => Array
        (
            [Couple] => orange
        )

    [1] => Array
        (
            [Couple] => apple
        )

    [2] => Array
        (
            [Couple] => banana
        )

)

答案 3 :(得分:0)

下面是一个基于foreach的简单解决方案,您可能想尝试一下。顺便说一下,你可以Quick-Test it here

<?php
    $array      = [
            ["Couple" => 5726],
            ["Couple" => 3421],
            ["Couple" => 3421],
            ["Couple" => 3421],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 5339],
            ["Couple" => 5339],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3421],
            ["Couple" => 5726],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 2213],
            ["Couple" => 6327],
            ["Couple" => 6327],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 3910],
            ["Couple" => 5339],
            ["Couple" => 5339],
            ["Couple" => 6327],
            ["Couple" => 6327],
            ["Couple" => 6327],
            ["Couple" => 6327],
            ["Couple" => 6327],
        ];

        $arrFinal   = [];

        foreach($array as $iKey=>$arrCouple){
            if(!in_array($arrCouple, $arrFinal)){
                $arrFinal[] = $arrCouple;
            }else{
                // DO WHATEVER YOU WANT TO DO
                // IN THE CASE THE DATA DOES ALREADY EXIST...
                // THIS WOULD BE UP TO YOU...
            }
        }

        var_dump($arrFinal);

var_dump($arrFinal);收益率:

        array (size=6)
          0 =>
            array (size=1)
              'Couple' => int 5726
          1 =>
            array (size=1)
              'Couple' => int 3421
          2 =>
            array (size=1)
              'Couple' => int 2213
          3 =>
            array (size=1)
              'Couple' => int 5339
          4 =>
            array (size=1)
              'Couple' => int 3910
          5 =>
            array (size=1)
              'Couple' => int 6327