Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在多维数组中搜索php部分键并删除过时的数组级别_Php_Arrays_Partial - Fatal编程技术网

在多维数组中搜索php部分键并删除过时的数组级别

在多维数组中搜索php部分键并删除过时的数组级别,php,arrays,partial,Php,Arrays,Partial,在看了很多关于php数组的问题后,我仍然无法解决我的问题 我的目标是搜索部分匹配的数组,并将这些结果存储到新数组中。我处理的数据是国家信息、地区信息和城市信息 我有3个SQL查询返回如下数据: [名称][行动][价值] 阿姆斯特丹,城市,阿姆斯特丹 荷兰 荷兰,国家,荷兰 检索结果,然后使用 $smartsearcharray = array(); array_push($smartsearcharray, $results_countries); array_push($smartsearch

在看了很多关于php数组的问题后,我仍然无法解决我的问题

我的目标是搜索部分匹配的数组,并将这些结果存储到新数组中。我处理的数据是国家信息、地区信息和城市信息

我有3个SQL查询返回如下数据: [名称][行动][价值] 阿姆斯特丹,城市,阿姆斯特丹 荷兰 荷兰,国家,荷兰

检索结果,然后使用

$smartsearcharray = array();
array_push($smartsearcharray, $results_countries);
array_push($smartsearcharray, $results_provinces);
array_push($smartsearcharray, $results_cities);
但是数组看起来像

Array
(
[0] => Array
    (
        [0] => Array
            (
                [name] => andorra
                [action] => country
                [value] => AD
            )

        [1] => Array
            (
                [name] => argentina
                [action] => country
                [value] => AR
            )

[1] => Array
    (
        [0] => Array
            (
                [name] => canillo
                [action] => province
                [value] => AD-02
            )

        [1] => Array
            (
                [name] => andorra la vella
                [action] => province
                [value] => AD-07
            )
尝试合并数组0和数组1,因为它们包含相同的结构,但使用RecursiveIteratorIterator失败

我尝试使用strpos、preg_grep和组合递归地排列walk,但由于额外的数组维度,它们经常失败

对于我应该使用的功能或技术有什么建议吗

非常感谢

/编辑:解决方案


第1部分,通过数组合并而不是数组推送求解数组

$results_countries = $this->model_search_smartsearch->buildCountries();
$results_provinces = $this->model_search_smartsearch->buildProvinces();
$results_cities = $this->model_search_smartsearch->buildCities();

$smartsearcharray = array();
$smartsearcharray = array_merge($results_countries, $results_provinces, $results_cities );
第2部分,搜索数组值,如果匹配,则将信息存储在较小/新的数组中


试试这个:那么,您是在尝试合并这三个数组吗?将它们合并到一个数组中,而不是将它们添加到新数组中?array\u merge可能就是您想要的:难道您不能以更方便的格式从数据源请求/获取记录吗?感谢提示,我现在有了更简单的数组,这很好。我添加了php代码,但是这与正确的键名匹配,但没有给我一个选项来获取存储在数组中的操作和值。
$results_countries = $this->model_search_smartsearch->buildCountries();
$results_provinces = $this->model_search_smartsearch->buildProvinces();
$results_cities = $this->model_search_smartsearch->buildCities();

$smartsearcharray = array();
$smartsearcharray = array_merge($results_countries, $results_provinces, $results_cities );
$res_arr = array();

foreach($smartsearcharray as $item)
{
    if(stripos($item['name'], $search_word) !== false) {

    $res_arr[] = array(
            'name'      => $item['name'],
            'action'    => $item['action'],
            'value'     => $item['value']
            );
    }
}

print_r($res_arr);