Php Array_Search问题

时间:2014-02-03 00:59:58

标签: php

我一直在尝试使用array_search函数。

这是我的阵列:

array (size=23)
  0 => 
    array (size=3)
      0 => string '15' (length=2)
      1 => float 115.08386533184
      2 => string '2014-02-06 21:00:00' (length=19)
  1 => 
    array (size=3)
      0 => string '81' (length=2)
      1 => float 100.41587590619
      2 => string '2014-03-28 00:00:00' (length=19)
  2 => 
    array (size=3)
      0 => string '65' (length=2)
      1 => float 99.096448338334
      2 => string '2014-02-08 21:00:00' (length=19)
  3 => 
    array (size=3)
      0 => string '53' (length=2)
      1 => float 98.752479251378
      2 => string '2014-03-05 12:00:00' (length=19)
  4 => 
    array (size=3)
      0 => string '24' (length=2)
      1 => float 98.303557178126
      2 => string '2014-02-07 21:00:00' (length=19)
  5 => 
    array (size=3)
      0 => string '23' (length=2)
      1 => float 98.270536817788
      2 => string '2014-02-08 21:30:00' (length=19)
  6 => 
    array (size=3)
      0 => string '37' (length=2)
      1 => float 98.139812350661
      2 => string '2014-02-06 23:00:00' (length=19)
  7 => 
    array (size=3)
      0 => string '13' (length=2)
      1 => float 97.810954038756
      2 => string '2014-02-09 19:45:00' (length=19)
  8 => 
    array (size=3)
      0 => string '22' (length=2)
      1 => float 95.975221202728
      2 => string '2014-02-11 20:30:00' (length=19)
  9 => 
    array (size=3)
      0 => string '66' (length=2)
      1 => float 94.115075316114
      2 => string '2014-02-04 19:00:00' (length=19)
  10 => 
    array (size=3)
      0 => string '52' (length=2)
      1 => float 93.957291067159
      2 => string '2014-03-15 00:00:00' (length=19)
  11 => 
    array (size=3)
      0 => string '12' (length=2)
      1 => float 89.188128768086
      2 => string '2014-05-17 10:00:00' (length=19)
  12 => 
    array (size=3)
      0 => string '51' (length=2)
      1 => float 88.381986169995
      2 => string '2014-03-27 12:00:00' (length=19)
  13 => 
    array (size=3)
      0 => string '98' (length=2)
      1 => float 85.240471279545
      2 => string '2014-02-17 18:00:00' (length=19)
  14 => 
    array (size=3)
      0 => string '92' (length=2)
      1 => float 82.721601210972
      2 => string '2014-02-06 20:15:00' (length=19)
  15 => 
    array (size=3)
      0 => string '82' (length=2)
      1 => float 82.473535719129
      2 => string '2014-02-27 17:00:00' (length=19)
  16 => 
    array (size=3)
      0 => string '121' (length=3)
      1 => float 56.833974620724
      2 => string '2014-02-07 22:30:00' (length=19)
  17 => 
    array (size=3)
      0 => string '120' (length=3)
      1 => float 36.269423317467
      2 => string '2014-02-06 21:30:00' (length=19)
  18 => 
    array (size=3)
      0 => string '83' (length=2)
      1 => float 35.863780709688
      2 => string '2014-02-21 07:00:00' (length=19)
  19 => 
    array (size=3)
      0 => string '35' (length=2)
      1 => float 24.322967350005
      2 => string '2014-02-07 21:30:00' (length=19)
  20 => 
    array (size=3)
      0 => string '6' (length=1)
      1 => float 19.71405484708
      2 => string '2014-02-14 22:00:00' (length=19)
  21 => 
    array (size=3)
      0 => string '45' (length=2)
      1 => float 19.142717452481
      2 => string '2014-03-15 00:00:00' (length=19)
  22 => 
    array (size=3)
      0 => string '117' (length=3)
      1 => float 17.641278106673
      2 => string '2014-02-18 17:30:00' (length=19)

现在 - 问题是我正在使用array_search来获取特定ID的密钥(注意:ID是每个数组中的第一个值)。问题是,它不起作用,可能是因为它无法获取数组本身的密钥。

让我举个例子:( $ array =上面的数组)

$r = array_search(53, array_keys($array));

我希望$ r等于3,因为这就是数组的关键位置。 知道怎么做吗?

4 个答案:

答案 0 :(得分:3)

$r = array_search('53', array_map(function($x) { return $x[0]; }, $array));

array_map将构造数组中每个元素的元素0数组。然后,您在此搜索53$r将成为其位置。

或者你可以简单地写一个循环:

foreach ($array as $i => $el) {
    if ($el[0] == '53') {
        $r = $i;
        $result = $el;
        break;
    }
}

答案 1 :(得分:0)

我搜索了SO并找到了类似的主题:PHP Multidimensional Array - Search for value and get the sub-array

我略微修改了上面提供的解决方案之一。

$id=53;
$r=null;
$data=null;

foreach ($array as $idx=>$subArray) {
  if ( $subArray[0] == $id ) {
    $r=$idx;
    $target=$subArray;
    break;
  }
}

答案 2 :(得分:0)

使用array_map functionarray_filter function

function find($a, $id) {
    return ($a[0] == $id) ? $a : false;
}
function filter($a) {
    return $a;
}

$r = array_filter(array_map("find", $array, array(53)), "filter");

答案 3 :(得分:0)

array_map()更好的调用来隔离第一列值:array_column()。这是为该函数创建的任务。

使用此实现:

$r = array_search('53', array_column($array,0));

完成。