• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Data to a MultiDimensional Array...

#1
Hey there---

I'm looking for help on how to do this...

I have the following array:
Array ( 
[0] => Array ( 
[ID] =>123
[Car] => Honda 
[Make] => Accord 
[Color] => White 

[1] => Array ( 
[ID] =>654
[Car] => Toyota 
[Make] => Camry
[Color] => Black 

[2] => Array ( 
[ID] =>789
[Car] => Nissan 
[Make] => Altima
[Color] => Red 

)

All in this variable:
$data['get_cars'] = $car_query->result_array();


Then I do a foreach with the IDs to grab the years...
Year of Honda with ID 123 is 2008
Year of Toyota with ID 654 is 2010
Year of Nissan with ID 789 is 2014

Now i have an array like this:
Array ( 
[0] => Array ( 
[Year] =>2010

[0] => Array ( 
[Year] =>2012

[0] => Array ( 
[Year] =>2015

)
How do I do this so I can add this to my original multidimensional array?
Reply

#2
In PHP you can use array keys to do a lot of work for you.

PHP Code:
// fetch list of cars
$cars $car_query->result_array();

// get all returned car IDs
$ids = [];
foreach (
$cars as $data) {
    
$ids[$data['ID']] = $data['ID'];
}

// user car IDs to get years, where getYearsArrayWithIDs is your query or other function
// you should return both ID and Year instead of just Year
$years getYearsArrayWithIDs($ids);

// make new array where key is Car ID and value is year
$idYears = [];
foreach (
$years as $data) {
   
$idYears[$data['ID']] = $data['Year'];
}

/*results 
[
    '123' => '2008',
    '654' => '2010',
    '789' => '2014'
]
*/

// loop over cars again and assign all years

foreach ($cars as $i => $data) {
    
// double check we have key/value in idYears before trying to access it
    
if (isset($idYears[$data['ID']])) {
        
$cars[$i]['Year'] = $idYears[$data['ID']];
    } else {
        
$cars[$i]['Year'] = false;
    }


Another option you can try, assuming the years come from DB too, is to join two tables when you do the original query.
Reply

#3
Hey Pertti--thank you so much for your response...I'm trying to wrap my brain around this...

Ok, so when i make the transition from Car Id's to get the Years my sql will look something like this(keeping it simple)

$sql= $this->db->query(" SELECT y.CarID, y.Year FROM Years y WHERE y.CarID = ????$CarID????")

How do I do the variable for CarID in the sql query?
Reply

#4
The SQL statement you are looking for is IN. This function will match field with any value from comma separated array, so your SQL would look something like this:

Code:
SELECT y.CarID, y.Year FROM Years y WHERE y.CarID IN (123, 654, 789);

In CodeIgniter, there are handy way to implement this functionality with query builder:

PHP Code:
// check that we have any cars to match against first
if ($carIDs) {
    
$query $this->db
        
->select('CarID, Year')
 
       ->where_in('CarID'$carIDs)
 
       ->get('Years');
 
   print_r($query->result());

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.