Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, April 4, 2016

using array_search for multi dimensional array

using array_search for multi dimensional array


using array_search in a 1 dimensional array is simple

$array = array("apple", "banana", "cherry");  $searchValue = "cherry";  $key = array_search($searchValue, $array);    echo $key;  

but how about an associative array?

    #RaceRecord        [CarID] [ColorID] [Position]  [0]    1        1         3  [1]    2        1         1  [2]    3        2         4  [3]    4        2         2  [4]    5        3         5  

for example i want to get the index of the car whose position is 1. How do i do this?

Answer by Amber for using array_search for multi dimensional array


function find_car_with_position($cars, $position) {      foreach($cars as $index => $car) {          if($car['Position'] == $position) return $index;      }      return FALSE;  }  

Answer by SLAYER for using array_search for multi dimensional array


actually all array functions are designed for single dimension array.You always need to keep in mind that you are applying it on single dimension array.

function find_car_with_position($cars, $position) {      for($i=0;$i

Answer by Michael J. Calkins for using array_search for multi dimensional array


I basically 'recreated' underscore.js's findWhere method which is to die for.

The function:

function findWhere($array, $matching) {      foreach ($array as $item) {          $is_match = true;          foreach ($matching as $key => $value) {                if (is_object($item)) {                  if (! isset($item->$key)) {                      $is_match = false;                      break;                  }              } else {                  if (! isset($item[$key])) {                      $is_match = false;                      break;                  }              }                if (is_object($item)) {                  if ($item->$key != $value) {                      $is_match = false;                      break;                  }              } else {                  if ($item[$key] != $value) {                      $is_match = false;                      break;                  }               }          }            if ($is_match) {              return $item;             }      }        return false;  }  

Example:

$cars = array(      array('id' => 1, 'name' => 'Toyota'),      array('id' => 2, 'name' => 'Ford')  );    $car = findWhere($cars, array('id' => 1));  

or

$car = findWhere($cars, array(      'id' => 1,      'name' => 'Toyota'  ));  

I'm sure this method could easily reduce LOC. I'm a bit tired. :P

Answer by Loupax for using array_search for multi dimensional array


Hooray for one-liners!

$index = array_keys(array_filter($array, function($item){ return $item['property'] === 'whatever';}))[0];  

Answer by SRB for using array_search for multi dimensional array


In php 5.5.5 & later versions, you can try this

$array_subjected_to_search =array(  array(          'name' => 'flash',          'type' => 'hero'      ),    array(          'name' => 'zoom',          'type' => 'villian'      ),    array(          'name' => 'snart',          'type' => 'antihero'      )  );  $key = array_search('snart', array_column($array_subjected_to_search, 'name'));  var_dump($array_subjected_to_search[$key]);  

working sample : http://sandbox.onlinephpfunctions.com/code/19385da11fe0614ef5f84f58b6dae80bd216fc01


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.