Welcome Guest, Not a member yet? Register   Sign In
Multidimensional array generate
#2

[eluser]darkhouse[/eluser]
Why does it have to be multidimensional? What's wrong with doing this?

Code:
foreach ($items as &$item){
   $item['distance'] = $this->cfm_lib->getDistance($lat[0]->Lat, $long[0]->Long, $item->Lat, $item->Long);
}

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++){
   $items[$i]['distance'] = $this->cfm_lib->getDistance($lat[0]->Lat, $long[0]->Long, $items[$i]->Lat, $items[$i]->Long);
}

Same thing, really. I just prefer the foreach method, it's cleaner.


Messages In This Thread
Multidimensional array generate - by El Forum - 07-12-2009, 12:41 PM
Multidimensional array generate - by El Forum - 07-12-2009, 01:33 PM
Multidimensional array generate - by El Forum - 07-12-2009, 02:17 PM
Multidimensional array generate - by El Forum - 07-12-2009, 03:10 PM
Multidimensional array generate - by El Forum - 07-12-2009, 03:10 PM
Multidimensional array generate - by El Forum - 07-12-2009, 03:11 PM
Multidimensional array generate - by El Forum - 07-12-2009, 03:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB