简体   繁体   中英

in array for multidimensional array in php

My first array is the needle:-

$needle = Array
(
    [2] => authenticated user
    [3] => administrator
    [13] => first keyword
    [23] => second keyword
    [33] => third keyword
)

Second array where this needle should be matched is a multi-dimensional array:-

$hay = Array
(
    [0] => Array
        (
            [value] => first keyword
        )

    [1] => Array
        (
            [value] => second keyword
        )

    [2] => Array
        (
            [value] => tenth keyword
        )

)

I am hoping that if needle array matches the hay array, I should get true.

I tried using this but it doesn't work:-

if (in_array(strtolower($hay), $needle)) {
...
}

Thanks.

$needle = array
(
    2 => 'authenticated user',
    3 => 'administrator',
    13 => 'first keyword',
    23 => 'second keyword',
    33 => 'third keyword'
);
$hay = array
(
    0 => array
        (
            'value' => 'first keyword'
        ),

    1 => array
        (
            'value' => 'second keyword'
       ),

    2 => array
        (
            'value' => 'tenth keyword'
        )

);

function check_array($array1, $array2){
    $result = array();
    foreach($array1 as $key => $val) {
         if(isset($array2[$key])){
           if(is_array($val) && $array2[$key]){
               $result[$key] = check_array($val, $array2[$key]);
           }
       } else {
           $result[$key] = $val;
       }
    }

    return $result;
}
if (check_array($hay, $needle)) {
    print_r($result);
}

Unfortunately PHP does not allow the function in_array on muli dimensional arrays. You need to write your own function in_array_multi which iterates though all the members of the array in all dimensions.

<?php

function in_array_multi($needle, $hay, $strict = FALSE) {
    if (is_array($hay)) {

        foreach ($hay as $hay2) {
            $result = FALSE;
            if (
                is_array($hay2)
            ) {
                if ($result = in_array_multi($needle, $hay2, $strict)) {
                    return $result;
                } else {
                    continue;
                }
            } else if (is_array($needle)) {
                if ($result = in_array_multi($hay2, $needle, $strict)) {
                    return $result;
                } else {
                    continue;
                }
            } else if ($strict && $hay2 === $needle) {
                return TRUE;
            } else if (!$strict && $hay2 == $needle) {
                return TRUE;
            }
        }
    }

    return FALSE;
}

$needle = Array
(
    2 => "authenticated user",
    3 => "administrator",
    13 => "first keyword",
    23 => "second keyword",
    33 => "third keyword"
);

// Second array where this needle should be matched is a multi-dimensional array:-

$hay = Array
(
    0 => Array
        (
            "first keyword"
        ),

    1 => Array
        (
            "second keyword"
        ),

    2 => Array
        (
            "tenth keyword"
        ),
);

$lowerNeedle = array();
foreach ($needle as $k => $v) {
    $lowerNeedle[] = strtolower($v);
}

if (in_array_multi($lowerNeedle, $hay) !== FALSE) {
echo "The needle '" . implode(",", $needle) . "' has been found.";
} else {
    echo "Needle has not been found.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM