2013-09-05 2 views
1

값을 반환하는 것이 아니라 "거리"키에 배열이있는 배열을 반환하려고합니다.배열 전체 배열 반환 (최대 값 포함)

즉. 에서 :

[0] => Array 
    (
     [pid] => 1 
     [type] => lj 
     [distance] => 211849216 
     [maxspeed] => 277598944 
     ... 
    ) 
[1] => Array 
    (
     [pid] => 1 
     [type] => lj 
     [distance] => 230286752 
     [maxspeed] => 289118816 
     ... 
    ) 
[2] => Array 
    (
     [pid] => 1 
     [type] => lj 
     [distance] => 230840928 
     [maxspeed] => 298438336 
     ... 
    ) 
... 

나는 [2]을 얻기 위해 소원 : 내가 가진 최대 가치를 얻을 수있었습니다

(
    [pid] => 1 
    [type] => lj 
    [distance] => 230840928 
    [maxspeed] => 298438336 
    ... 
) 

다음

function max_dist($a) { 
    return $a["distance"]; 
} 
$jump = max(array_map("max_dist", $jumps))); 

그러나 그것이 얼마나 우아하게 간단한 달리 JS 포함/밑줄 :

var jump=_.max(jumps,function(o){ 
    return +o.distance; 
}); 

최대 거리 값만 반환합니다.

PHP에서 간단하게 뭔가를 놓치고 있어야합니다!

답변

1
function max_dist($array) { 
    $maxIndex = 0; 
    $index = 0; 
    $maxValue = $array[0]['distance']; 
    foreach($array as $i){ 
     if($i['distance'] > $maxValue){ 
      $maxValue = $i['distance']; 
      $maxIndex = $index; 
     } 
    $index++; 
    } 
    return $array[$maxIndex]; 
} 
$jump = max(array_map("max_dist", $jumps))); 
+0

JS에있는 것처럼 우아하고 단순하지는 않지만 잘된 것 같습니다. 빠른 응답 주셔서 감사합니다! – tenub

+0

이 코드는'function max_dist ($ array)'에 추가 될 예정이지만 모바일에서 이것을 편집하는 데 문제가 있습니다 : D –