php数组操作

时间:2011-10-03 17:14:13

标签: php arrays max extraction

我有以下2D数组,我想比较一些值。首先,我想从数组中获取最高值,并依赖于使用相同索引从前一个值中提取时间。

示例:最高(work_something(223)) - 上一个(work_something(120))

$data = array(
  0 => array(
    'time' => '2011-10-03 18:43:00',
  ),
  1 => array(
    'time' => '2011-10-03 18:44:00',
  ),
  2 => array(
    'time' => '2011-10-03 18:45:00',
    'item_something' => -17,
    'keyword_something' => 0,
    'keyword_something_1' => 0,
    'search_something' => 0,
    'search_links_something' => 0,
    'work_something' => 120,
  ),
  3 => array(
    'time' => '2011-10-03 18:45:00',
    'item_something' => -17,
    'keyword_something' => 0,
    'keyword_something_1' => 0,
    'search_something' => 0,
    'search_links_something' => 0,
    'work_something' => 223,
  ),
);

1 个答案:

答案 0 :(得分:0)

$array //This is your 2D array

$highest = 0;
foreach($array as $key => $value):
  if(isset($value['work_something'])){
    if($value['work_something'] > $highest){
    $highest = $value['work_something']; //This is highest 'work_something' value
    $highest_array_key = $key; //This is the key of 'work_something' array
    }
  }
endforeach;

//To compare with previews element use 
$array[$highest_array_key - 1] //this is prew element

示例:最高(work_something(223)) - 上一个(work_something(120))

$highest - $array[$highest_array_key - 1]['work_something'];