Frage

After looking at many of the already asked questions on php array's I still cant resolve my issue.

My goal is to search an array for partial match and store these results to a new array. The data I'm working with is country information, region information and city information.

I have 3 SQL queries that return data like: [name][action][value] amsterdam,city,amsterdam nederland,country,NL netherlands,country,NL

The results are retrieved and then merged into an array with

$smartsearcharray = array();
array_push($smartsearcharray, $results_countries);
array_push($smartsearcharray, $results_provinces);
array_push($smartsearcharray, $results_cities);

However the array looks like

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
            )

Attempts to merge array 0 and array 1, because they contain the same structure failed with RecursiveIteratorIterator

I tried to array_walk recursively, using strpos, preg_grep and combinations but they fail often due to the extra array dimention.

Any suggestions for functions or techniques I should use to get this working?

Many thanks

/edit: Solution

$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);
War es hilfreich?

Lösung

Part 1, solving the array via the array merge instead of array_push

$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 );

Part 2, searching the array value, and if it matches store the information in a smaller /new array

$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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top