![]() |
Multidimensional array generate - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Multidimensional array generate (/showthread.php?tid=20506) |
Multidimensional array generate - El Forum - 07-12-2009 [eluser]karloff[/eluser] Hi, i'm trying to create a search feature which bring back results of how close you are to a particular place. my problem is that I need to add the info of each place into a multidimensional array, with the added field of the distance, so i can sort it.... this is my code so far but It keep generating php errors, Is this possible or am i going about it the wrong way? Code: $item = $this->cfmmodel->get_all_places(); Multidimensional array generate - El Forum - 07-12-2009 [eluser]darkhouse[/eluser] Why does it have to be multidimensional? What's wrong with doing this? Code: foreach ($items as &$item){ Notice the & in front of $item in the foreach statement. This references each $item so it will affect the $items array. But now you just have one array with a distance value for each element. Or, if you're using php4 still, you could do this: Code: for($i = 0; $i < count($items); $i++){ Same thing, really. I just prefer the foreach method, it's cleaner. Multidimensional array generate - El Forum - 07-12-2009 [eluser]karloff[/eluser] hey man, this sounds ideal but i'm having trouble implementing it Code: Cannot use object of type stdClass as array in ...... why would that be?? ps. its php5 but thanks for the insight for the diff between php 4 and 5 ![]() Multidimensional array generate - El Forum - 07-12-2009 [eluser]darkhouse[/eluser] Oh try this instead... Code: foreach ($items as &$item){ Multidimensional array generate - El Forum - 07-12-2009 [eluser]karloff[/eluser] got it sorted Code: $item->distance = $this->cfm_lib->getDistance($lat[0]->Lat, $long[0]->Long, $item->Lat, $item->Long); Multidimensional array generate - El Forum - 07-12-2009 [eluser]karloff[/eluser] ha ha, thanks for the reply, i just posted the exact same thing as you at the same time.... made me laugh... cheers Multidimensional array generate - El Forum - 07-12-2009 [eluser]darkhouse[/eluser] Haha yeah, I just posted that. I shoulda caught that when I first posted. |