php - algorithm to find places in sorted array -
php - algorithm to find places in sorted array -
i have sorted array of keys , value like
array ( [1] => array ( [value] => 65 ...... ) [0] => array ( [value] => 65 ) [5] => array ( [value] => 35 ) [4] => array ( [value] => 3 ) [2] => array ( [value] => 0 ) [3] => array ( [value] => 0 ) )
i search simple algorithm run on array , homecoming array of places :
places['1'] = keys => array(1,0), value => 65 places['2'] = keys => array(5), value => 35 places['3'] = keys => array(4), value => 3 places['4'] = keys => array(2,3), value => 0
so in first place have key 1 + 0, , value 65 , on.......
i seek loop foreach
, add together lot of if
conditions, search simple
thanks
i assume mean places['4']
in lastly line. if so, should (otherwise have explain why there 2
;)):
$placeindex = 0; $lastvalue = null; $places = array(); foreach($array $key => $value) { if($value['value'] === $lastvalue) { $places[$placeindex]['keys'][] = $key; } else { $placeindex++; $lastvalue = $value['value']; $places[$placeindex] = array( 'keys' => array($key), 'value' => $lastvalue ); } }
php arrays
Comments
Post a Comment