php - Removing names in array by distance -
i have multidimensional array of venues so:
[0] => array ( [dist] => 5.421905274077098 [name] => venue1 ) [1] => array ( [dist] => 1.6506176672720143 [name] => venue2 ) [2] => array ( [dist] => 0.9541989204006235 [name] => venue1 ) i attempting remove same name, priority on distance, in illustration above 3rd item stay, , first item removed. far have attempted looping through array twice , unsetting have same name , less distance, not appear remove them
for($i=0 ; $i<count($returned) ; $i++) { for($j=0 ; $j<count($returned) ; $j++) { if(!$returned[$j] || !$returned[$i]) { } else { if($returned[$j]['name']==$returned[$i]['name']) { if($returned[$j]['dist']<$returned[$i]['dist']) { unset($returned[$i]); } } } } }
you can utilize temporary array this:
$result = array(); foreach ($returned $item) { $name = $item['name']; // convenience variable if (!isset($result[$name]) || $result[$name]['dist'] > $item['dist']) { // add together or overwrite $result[$name] = $item; } } afterwards, $result array contain items after removal.
php arrays
No comments:
Post a Comment