![]() |
Adding Data to a MultiDimensional Array... - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Adding Data to a MultiDimensional Array... (/showthread.php?tid=71178) |
Adding Data to a MultiDimensional Array... - ryan_f - 07-16-2018 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? RE: Adding Data to a MultiDimensional Array... - Pertti - 07-16-2018 In PHP you can use array keys to do a lot of work for you. PHP Code: // fetch list of cars Another option you can try, assuming the years come from DB too, is to join two tables when you do the original query. RE: Adding Data to a MultiDimensional Array... - ryan_f - 07-17-2018 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? RE: Adding Data to a MultiDimensional Array... - Pertti - 07-17-2018 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 |