Welcome Guest, Not a member yet? Register   Sign In
Another multidimentional array question
#1

[eluser]mabright[/eluser]
I tried to avoid posting this as there are many other similar post but I have not found a solution.

Description: In my controller there are 2 processes; process 1 queries a list of nearby cities based on IP address. Process 2 finds venues located in the cities returned by process step 1.

In step 2 I loop through my array of cities while calling a model to get a list of venues for the current city(below):

Controller Code:
Code:
foreach($nearby_regions as $area)
        {
            $arr = $this->Venue_model->get_venues($area['city'],$area['region']);
            if (empty($arr))
            {
                continue;
            }
            $venues[] = $arr;
        }

Model Code:
Code:
function get_venues($city,$state)
    {        
        $venues = array();
        
        $sql = "SELECT * FROM `venue`
                WHERE UPPER(`city`) = ?
                AND   UPPER(`state`) = ?";
        $query = $this->db->query($sql, array(strtoupper(trim($city)),strtoupper(trim($state))));
        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $venues[] = array('venue_id'         => $row->venue_id,
                                  'venue_name'       => $row->venue_name,
                                  'venue_desc'       => $row->venue_desc);
            }
        }
        return $venues;
    }
My issue is that the array is deep and I want the venue data to be at the first level, instead it is at the 3rd level. My logic is working but I need a for loop before my for-each loop just to get to the data. See below.

Code:
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [venue_id] => 10027
                    [venue_name] => Test venue 1
                    [venue_desc] => Bar & Grill
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [venue_id] => 10022
                    [venue_name] => Test venue 2
                    [venue_desc] => abc 123
                )

            [1] => Array
                (
                    [venue_id] => 10029
                    [venue_name] => Test vinue 3
                    [venue_desc] => abc 123
                 )

        )

)
#2

[eluser]WanWizard[/eluser]
Merge the results, instead of adding them to an array as separate elements. In your controller, replace
Code:
$venues[] = $arr;
by
Code:
$venues = array_merge($venues, $arr);
#3

[eluser]tonanbarbarian[/eluser]
your simplest option all round would be to grab all the data in one query using joins rather than in multiple queries

1 query is always better than dozens
dont know how many cities the first query is likely to find, but if it could potentially find hundreds you are going to have hundreds of queries being run
#4

[eluser]mabright[/eluser]
Thanks WanWizard. I was searching through the PHP Array functions and this is just what I was looking for, works perfectly.

tonanbarbarian,

I will try to merge the queries, I initially thought of this but wanted to keep the functions separate. I have a custom library for all my GEO location related functions which uses a location model and external APIs. My venue related data is in my venue model which is where my venue query happens and I do not want to add GEO location logic to this model.

I think I will break down the state/city array and build a string of states and a string of cities and pass this to the "get_venues" function instead of a single city and state.

This would make the query as:
Code:
SELECT * FROM `venue`
WHERE UPPER(`city`)  IN ('SAN JOSE','ATLANTA')
AND   UPPER(`state`) IN ('CA','GA');




Theme © iAndrew 2016 - Forum software by © MyBB