📜  PHP多维数组按值搜索

📅  最后修改于: 2022-05-13 02:24:09.615000             🧑  作者: Mango

PHP多维数组按值搜索

在PHP,多维数组搜索是指在多级嵌套数组中搜索值。有多种技术可以执行此类搜索,例如迭代嵌套数组、递归方法和内置数组搜索函数。

迭代方法:
遍历数组并搜索重要匹配项是可以遵循的最简单的方法。检查给定数组的元素本身是否是数组并将元素添加到搜索路径,否则在嵌套数组上运行数组搜索。

例子:

 $val1) {
  
        $temp_path = $id_path;
          
        // Adding current key to search path
        array_push($temp_path, $key1);
  
        // Check if this value is an array
        // with atleast one element
        if(is_array($val1) and count($val1)) {
  
            // Iterating over the nested array
            foreach ($val1 as $key2 => $val2) {
  
                if($val2 == $search_value) {
                          
                    // Adding current key to search path
                    array_push($temp_path, $key2);
                          
                    return join(" --> ", $temp_path);
                }
            }
        }
          
        elseif($val1 == $search_value) {
            return join(" --> ", $temp_path);
        }
    }
      
    return null;
}
  
// Multidimensional array 
$gfg_array = array(
    array(
        'score' => '100',
        'name' => 'Sam',
        'subject' => 'Data Structures'
    ),
    array(
        'score' => '50',
        'name' => 'Tanya',
        'subject' => 'Advanced Algorithms'
    ),
    array(
        'score' => '75',
        'name' => 'Jack',
        'subject' => 'Distributed Computing'
    )
);
  
$search_path = searchForId('Advanced Algorithms',
                    $gfg_array, array('$'));
  
print($search_path);
  
?>
输出:
$ --> 1 --> subject

递归方法:
万一,当嵌套数组的级别增加时,编写此类程序和调试它们就变得困难了。在这种情况下,最好编写一个递归程序,该程序可以在不添加任何嵌套 for 循环的情况下干净地编写。



例子:

 0) {
          
        foreach($array as $key => $value) {
  
            $temp_path = $id_path;
              
            // Adding current key to search path
            array_push($temp_path, $key);
  
            // Check if this value is an array
            // with atleast one element
            if(is_array($value) && count($value) > 0) {
                $res_path = array_search_id(
                        $search_value, $value, $temp_path);
  
                if ($res_path != null) {
                    return $res_path;
                }
            }
            else if($value == $search_value) {
                return join(" --> ", $temp_path);
            }
        }
    }
      
    return null;
}
  
// Multidimensional (Three dimensional) array
$gfg_array = array(
    "school1" => array(
        "year" => "2017",
        "data" => array(
            'score' => '100',
            'name' => 'Sam',
            'subject' => 'Data Structures'
        )
    ),
      
    "school2" => array(
        "year" => "2018",
        "data" => array(
            'score' => '50',
            'name' => 'Tanya',
            'subject' => 'Advanced Algorithms'
        )
    ),
      
    "school3" => array(
        "year" => "2018",
        "data" => array(
            'score' => '75',
            'name' => 'Jack',
            'subject' => 'Distributed Computing'
        )
    )
);
  
$search_path = array_search_id('Jack', $gfg_array, array('$'));
print($search_path);
  
?>
输出:
$ --> school3 --> data --> name

使用 array_search() 方法进行多维数组搜索:
array_search() 是一个内置函数,用于搜索与给定数组列/键相关的给定值。此函数仅返回键索引而不是搜索路径。

例子:

 '100',
        'name'    => 'Sam',
        'subject' => 'Data Structures'
    ),
    array(
        'score'   => '50',
        'name'    => 'Tanya',
        'subject' => 'Advanced Algorithms'
    ),
    array(
        'score'   => '75',
        'name'    => 'Jack',
        'subject' => 'Distributed Computing'
    )
);
  
$id = array_search('50', array_column($gfg_array, 'score'));
echo $id;
  
?>
输出:
1

PHP是一种专门为 Web 开发设计的服务器端脚本语言。您可以按照此PHP教程和PHP示例从头开始学习PHP 。