PHP搜索数组的起点和终点值

时间:2014-07-04 12:23:13

标签: php arrays

我有一个数组如下:

Array (
[0] => Array (
  ...
)
[41] => Array (
  [name] => London 
  [company] => nhyt6t
  [top25_1] => 8.75912088
)
[42] => Array (
  [name] => Manchester
  [company] => gtr4rf
  [top25_1] => 6.56758398
)
[43] => Array (
  [name] => Leeds
  [company] => de3wsd6
  [top25_1] => 7.58675398
)
[44] => Array (
  [name] => Liverpool
  [company] => fe4rf56
  [top25_2] => 4.5697965
)
)

是否有可能(在PHP中)搜索数组并为top25_1的开始和结束带回数组索引(注意:它们始终按顺序分组) - 所以在这个例子中:

$start = 41;
$end = 43;

top25_1的位置每次生成数组时都会有所不同,因此需要搜索。

欢迎任何建议,反馈和协助。

4 个答案:

答案 0 :(得分:1)

遍历您的数组并检查数组是否包含您的密钥,如下所示:

$start = null;
$end = 0;
foreach($arrays as $k => $arr) {
  if (array_key_exists('top25_1', $arr)) {
    if ($start === null) { // only update with the first key
       $start = $k; // 41
    }

    if ($k > $end) { // if the array key is bigger than the last end, update.
       $k = $end; // 43
    }
  }
}

答案 1 :(得分:1)

你可以这样做:

$result = array_reduce(array_keys($array), function($c, $x) use ($array)
{
   if(!isset($c[$key = end(array_keys($array[$x]))]))
   {
      $c[$key] = array('start'=>$x, 'end'=>$x);
   }
   else
   {
      $c[$key]['end'] = $x;
   };
   return $c;
}, array());

因此,上述语句的结果将是数组,其中top_XX为键,['start'=>Y, 'end'=>Z]为值,其中YZ为对应组的开始和结束。例如,如果输入数组是:

$array = array(
    41 => Array (
      'name' => 'London ',
      'company' => 'nhyt6t',
      'top25_1' => 8.75912088
    ),
    42 => Array (
      'name' => 'Manchester',
      'company' => 'gtr4rf',
      'top25_1' => 6.56758398
    ),
    43 => Array (
      'name' => 'Leeds',
      'company' => 'de3wsd6',
      'top25_1' => 7.58675398
    ),
    44 => Array (
      'name' => '--- ',
      'company' => 'nhyt6t',
      'top25_2' => 1
    ),
    45 => Array (
      'name' => '---',
      'company' => 'gtr4rf',
      'top25_2' => 1
    ),
    46 => Array (
      'name' => '??',
      'company' => 'de3wsd6',
      'top25_3' => 7.58675398
    )
);

然后结果将是:

array(3) {
  ["top25_1"]=>
  array(2) {
    ["start"]=>
    int(41)
    ["end"]=>
    int(43)
  }
  ["top25_2"]=>
  array(2) {
    ["start"]=>
    int(44)
    ["end"]=>
    int(45)
  }
  ["top25_3"]=>
  array(2) {
    ["start"]=>
    int(46)
    ["end"]=>
    int(46)
  }
}

这意味着您可以使用$result[$key]轻松获得所需的密钥开头和结尾(因此,$result['top25_1']代表您的问题)

请注意,上述陈述依赖于事实,即您的元素被分组 - 因此,它们不是 - 然后结果可能是意外的。

答案 2 :(得分:1)

$start = null;
$end = null;

while(list($index, $item) = each($array)) {
    if(isset($item['top25_1'])) {
        if($start === null) {
            $start = $index;
        }

        $end = $index;
    }
}

说明

Array (
    [0] => Array (
      ... // do nothing
    )
    [41] => Array (
        [name] => London 
      [company] => nhyt6t
      [top25_1] => 8.75912088 // set start and end
    )
    [42] => Array (
        [name] => Manchester
      [company] => gtr4rf
      [top25_1] => 6.56758398 // update end
    )
    [43] => Array (
        [name] => Leeds
      [company] => de3wsd6
      [top25_1] => 7.58675398 // update end
    )
    [44] => Array (
        [name] => Liverpool
      [company] => fe4rf56
      [top25_2] => 4.5697965 // do nothing
    )
)

答案 3 :(得分:0)

$init = -1;
$end = -1;
for($i=0;$i<count($array);$i++) {
  if ($init == -1 and array_key_exists('top25_1', $array[$i])) {
    $init=$i;

  }
  if($init>=0 and !array_key_exists('top25_1', $array[$i]))
  {
     $end=$i - 1;
     break;
  }

}
if($init>=0 and $end==-1)
   $end = count($array) -1;