0

I've been trying to work with the array_search function.

This is my array :

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)

Now - The thing is I'm using array_search in order to get the key of a specific ID (Note : The ID is the first value in each array). The thing is, it doesn't work, probably because it can't get the key of the array itself.

Let me show you an example : ($array = The array above)

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

I want $r to be equal to 3, because thats they array's key position. Any idea how I can do it?

3
  • 2
    When posting a structure like this, copy from the browser's page source so you retain linebreak and indentation formatting from print_r(),var_dump(). Then format it as a code block here, and it is far easier for us to comprehend visually. Commented Feb 3, 2014 at 1:01
  • 1
    array_keys($array) are just the indexes of the top-level array, which go from `0 to 22. What you want is to get element 0 of each sub-array, and search for 53 in that. Commented Feb 3, 2014 at 1:04
  • @Barmar I know, the thing is I'm trying to get the top-level array key position, by searching the ID in the sub-array Commented Feb 3, 2014 at 1:08

4 Answers 4

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

array_map will construct an array of element 0 of each element of the array. Then you search for 53 in this, and $r will be its position.

Or you could simply write a loop:

foreach ($array as $i => $el) {
    if ($el[0] == '53') {
        $r = $i;
        $result = $el;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I searched SO and found a similar topic: PHP Multidimensional Array - Search for value and get the sub-array.

I slightly modified one of the solutions provided above.

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

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

Comments

0

use array_map function and array_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");

Comments

0

A better call than array_map() to isolate the first column of values is: array_column(). This is the task that the function was created for.

Use this implementation:

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

Done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.