从 PHP 中的逗号分隔值搜索数组中的值


Search value in array from comma separated value in PHP

>我有以下数组

Array
(
    [0] => Array
        (
            [data] => PHP
            [attribs] => Array
                (
                )
            [xml_base] => 
            [xml_base_explicit] => 
            [xml_lang] => 
        )
    [1] => Array
        (
            [data] => Wordpress
            [attribs] => Array
                (
                )
            [xml_base] => 
            [xml_base_explicit] => 
            [xml_lang] => 
        )
)

像$var这样的变体='Php, Joomla';

我试过跟随但不起作用

$key = in_multiarray('PHP', $array,"data");
function in_multiarray($elem, $array,$field)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom][$field] == $elem)
                return true;
            else 
                if(is_array($array[$bottom][$field]))
                    if(in_multiarray($elem, ($array[$bottom][$field])))
                        return true;
            $bottom++;
        }        
        return false;
    } 

所以想检查数组中是否存在$var中的任何值(不区分大小写(

我怎么能做到没有循环?

这应该适合您:

(在代码中加入一些注释,解释发生了什么(

<?php
    //Array to search in
    $array = array(
                array(
                    "data" => "PHP",
                    "attribs" => array(),
                    "xml_base" => "",
                    "xml_base_explicit" => "", 
                    "xml_lang" => ""
                ),
                array(
                "data" => "Wordpress",
                "attribs" => array(),
                "xml_base" => "",
                "xml_base_explicit" => "",
                "xml_lang" => "Joomla"
                )
            );  
    //Values to search
    $var = "Php, Joomla";
    //trim and strtolower all search values and put them in a array 
    $search = array_map(function($value) {
                    return trim(strtolower($value));
    }, explode(",", $var));
    //function to put all non array values into lowercase
    function tolower($value) {
        if(is_array($value))
            return array_map("tolower", $value);
        else
            return strtolower($value);
    }
    //Search needle in haystack
    function in_array_r($needle, $haystack, $strict = false) {
        foreach ($haystack as $item) {
            if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
                return true;
            }
        }
        return false;
    }
    //Search ever value in array
    foreach($search as $value) {
        if(in_array_r($value, array_map("tolower", array_values($array))))
            echo $value . " found<br />";
    }
?>

输出:

php found
joomla found

据我了解,您正在尝试传递字符串ex:"php"和元素的键:"data"。所以你的键可以容纳一个值或一个数组。

    $key = in_multiarray("php", $array,"data");
    var_dump($key);
    function in_multiarray($elem, $array,$field)
    {
            $top = sizeof($array) - 1;
            $bottom = 0;
            while($bottom <= $top)
            {
                 if(is_array($array[$bottom][$field]))
                 {
                    foreach($array[$bottom][$field] as $value)
                    {
                        if(strtolower(trim($value)) == strtolower(trim($elem)))
                        {
                            return true;
                        }
                    }
                  }
                  else if(strtolower(trim($array[$bottom][$field])) == strtolower(trim($elem))) 
                  {
                    return true;
                  }
                $bottom++;
            }        
       return false;
    }