I have this array in PHP:
array(
[0] => array( \'username\' => \'user1\' )
[1] => array( \'username\' => \'user2\' )
)
Perhaps using array_filter and array_keys together will help.
Class Based Approach.
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:
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'));