get index of item in array by item name php code example
Example 1: get index of element in array php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
$key = array_search('red', $array);
Example 2: array_search
public $gender = array(
1 => 'Male',
2 => 'Female',
3 => 'Other',
);
$gen = 1;
if ($gen == array_search('Male', $this->gender)) {
$gender = __d('member', 'male');
} elseif ($gen == array_search('Female', $this->gender)) {
$gender = __d('member', 'female');
}
Example 3: get element by index array php
$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
$array = array_values($array);
echo $array[0];
echo $array[1];
echo $array[2];
Example 4: array_search in php
<?php
$a = [
'Canceled' => 1,
'Traded / Filled'=> 2,
'(Not used currently)'=> 3,
'Transit'=> 4,
'Rejected'=> 5,
'Pending'=> 6,
];
echo array_search("5",$a);
?>