array_keys ($array);
// It returns an array
// more informations at https://www.php.net/manual/fr/function.array-keys.php (fr)
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
foreach($yourArray as $key => $value) {
echo '<a href="' . $value . '">' . $key . '</a>';
}
// OR
//Use functions
key($array); //Returns current key
reset($array); //Moves array pointer to first record
current($array); //Returns current value
next($array); //Moves array pointer to next record and returns its value
prev($array); //Moves array pointer to previous record and returns its value
end($array); //Moves array pointer to last record and returns its value
$people = array(
2 => array(
'name' => 'John',
'fav_color' => 'green'
),
5=> array(
'name' => 'Samuel',
'fav_color' => 'blue'
));
$found_key = array_search('blue', array_column($people, 'fav_color'));
$i = array_search('blah', array_keys($array));
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
for($i = 0; $i< sizeof($array);$i++){
if (key($array[$i]) == 'apple') {
echo key($array).'<br />';
}
//next($array);
}
?>
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
// The easiest way
var_dump(json_decode(json_encode($value), true));