在另一个多维数组php中搜索数组值

时间:2014-12-05 11:28:06

标签: php search multidimensional-array

我有db array:

Array
(
    [28] => Array
        (
            [0] => child
            [1] => baby
            [2] => new d
            [3] => christmas
        )

    [29] => Array
        (
            [0] => christmas
        )

    [30] => Array
        (
            [0] => business
            [1] => stock
        )

    [32] => Array
        (
            [0] => apparel
            [1] =>  clothing
            [2] =>  contemporary apparel
        )

    [49] => Array
        (
            [0] => car rental
            [1] => car rent
            [2] => rent car
            [3] => rent a car
            [4] => car rentals
            [5] => car1 rentals1
        )
)

我有另一个阵列 在上面的数组中搜索此数组值     数组("圣诞节","服装");

所以我想要结果:28,29,32(这是数组的关键)

2 个答案:

答案 0 :(得分:4)

试试这个 -

$b = array("christmas","apparel");
$keys = array();
foreach ($dbArray as $key => $values) {
   $check = array_intersect($values, $b);
   if (!empty($check)) {
       $keys[] = $key;
   }
}

答案 1 :(得分:0)

下面是一个在多维数组中搜索值的递归函数。

function search_in_array($srchvalue, $array)
    {
        if (is_array($array) && count($array) > 0)
        {
            $foundkey = array_search($srchvalue, $array);
            if ($foundkey === FALSE)
            {
                foreach ($array as $key => $value)
                {
                    if (is_array($value) && count($value) > 0)
                    {
                        $foundkey = search_in_array($srchvalue, $value);
                        if ($foundkey != FALSE)
                            return $foundkey;
                    }
                }
            }
            else
                return $foundkey;
        }
    }

它将返回找到值的键。 如果有帮助,请不要忘记接受答案