Using preg_match on a multidimensional array to return key values arrays

后端 未结 6 985
情话喂你
情话喂你 2020-12-17 05:13

I have an array that is structured as such:

$data = array(
    \"abc\"=>array(
            \"label\" => \"abc\",
            \"value\" => \"def\",
          


        
6条回答
  •  半阙折子戏
    2020-12-17 05:53

    Something like this?

    array(
                "label" => "abc",
                "value" => "def",
                "type" => "ghi",
                "desc" => "jkl",
                ),
        "def"=>array(
                "label" => "mno",
                "value" => "qrs",
                "type" => "tuv",
                "desc" => "wxyz",
                ),
        );
    
    $matches = array();
    $pattern = "/a/i";  //contains an 'a'
    //loop through the data
    foreach($data as $key=>$value){
        //loop through each key under data sub array
        foreach($value as $key2=>$value2){
            //check for match.
            if(preg_match($pattern, $value2)){
                //add to matches array.
                $matches[$key]=$value;
                //match found, so break from foreach
                break;
            }
        }
    }
    echo '
    '.print_r($matches, true).'
    '; ?>

提交回复
热议问题