Home » Php » Get index of element in an array by the value

Get index of element in an array by the value

Posted by: admin November 29, 2017 Leave a comment

Questions:

I have this array in PHP:

array(
    [0] => array( 'username' => 'user1' )
    [1] => array( 'username' => 'user2' )
)

If I have the ‘username’ string how can I get the index value as a number?

Example, if I have ‘user1’ how can I get 0?

Answers:

If you have a 2D array, like in your example, you’ll need to customise things a little:

function array_search2d($needle, $haystack) {
    for ($i = 0, $l = count($haystack); $i < $l; ++$i) {
        if (in_array($needle, $haystack[$i])) return $i;
    }
    return false;
}

$myArray = array(
    array( 'username' => 'user1' ),
    array( 'username' => 'user2' )
);
$searchTerm = "user1";

if (false !== ($pos = array_search2d($searchTerm, $myArray))) {
    echo $searchTerm . " found at index " . $pos;
} else {
    echo "Could not find " . $searchTerm;
}

If you wanted to search in just one particular field, you could alter the function to something like this:

function array_search2d_by_field($needle, $haystack, $field) {
    foreach ($haystack as $index => $innerArray) {
        if (isset($innerArray[$field]) && $innerArray[$field] === $needle) {
            return $index;
        }
    }
    return false;
}

Questions:
Answers:

Take a look at array_search.

From the PHP help file:

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

Questions:
Answers:

This would be very simple

private function getArrayKey($haystack, $needle)
{
  foreach($haystack as $key => $product)
  {
     if ($product['id'] === $needle)
     return $key;
  }

  return false;
}

Questions:
Answers:

Perhaps using array_filter and array_keys together will help.

Class Based Approach.

<?php

class ArraySearch2d {
  static protected $_key;
  static protected $_value;

  static function isMatch($element)
  {
    if (!is_array($element)) return false;
    return $element[self::$_key] == self::$_value;
  }

  static function filter(array $arrayToSearch, $key, $value)
  {
    if (!is_string($key)) throw new Exception("Array Key must be a string");
    self::$_key = $key;
    self::$_value = $value;
    return array_filter($arrayToSearch, 'ArraySearch2d::isMatch');
  }

  // to directly answer your question.
  static function getIndex(array $arrayToSearch, $key, $value)
  {
    $matches = self::filter($arrayToSearch, $key, $value);
    if (!count($matches)) return false;
    $indexes = array_keys($matches);
    return $indexes[0];
  }
}

$array = array("1"=>array('username'=>'user1'), "3"=>array('username'=>'user2'));

$matches = ArraySearch2d::filter($array, 'username', 'user2');
var_dump($matches);

$indexs = array_keys($matches);
var_dump($indexs);

// Demonstrating quick answer:
echo "Key for first 'username'=>'user1' element is: "
     .ArraySearch2d::getIndex($array, 'username', 'user1')."\n";

Produces:

array(1) {
  [3]=>
  array(1) {
    ["username"]=>
    string(5) "user2"
  }
}
array(1) {
  [0]=>
  int(3)
}
Key for first 'username'=>'user1' element is: 1

Without using classes – this produces the same result:

<?php
$field="username";
$value = "user2";

function usernameMatch($element)
{
   global $field, $value;
   if (!is_array($element)) return false; 
   return $element[$field] == $value;
}

function getFirstIndex(array $array)
{
  if (!count($array)) return false;
  $indexes = array_keys($array);
  return $indexes[0];
}

$array = array("1"=>array('username'=>'user1'), "3"=>array('username'=>'user2'));

$matches = array_filter($array, 'usernameMatch');
var_dump($matches);

$indexs = array_keys($matches);
var_dump($indexs);


// Demonstrating quick answer - and why you should probably use the class- 
// you don't want to have to remember these "globals" all the time.
$field = 'username';
$value = 'user1';

echo "Key for first 'username'=>'user1' element is: "
     .getFirstIndex(array_filter($array, 'usernameMatch'));

Questions:
Answers:

If you know that the key is username, just use an array as the search parameter:

$username = 'user1';
$key = array_search(array('username' => $username), $array);

Leave a Reply

Your email address will not be published. Required fields are marked *