1<?php
2$array = array(
3 'fruit1' => 'apple',
4 'fruit2' => 'orange',
5 'fruit3' => 'grape',
6 'fruit4' => 'apple',
7 'fruit5' => 'apple');
8
9$keys = array_keys($array); // return array
10$values = array_values($array); // return array
11?>
1
2<?php
3$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
4
5$key = array_search('green', $array); // $key = 2;
6$key = array_search('red', $array); // $key = 1;
7?>
8
9
1array_keys ($array);
2// It returns an array
3// more informations at https://www.php.net/manual/fr/function.array-keys.php (fr)
1$people = array(
2 2 => array(
3 'name' => 'John',
4 'fav_color' => 'green'
5 ),
6 5=> array(
7 'name' => 'Samuel',
8 'fav_color' => 'blue'
9 ));
10$found_key = array_search('blue', array_column($people, 'fav_color'));
1
2<?php
3$array = array(
4 'fruit1' => 'apple',
5 'fruit2' => 'orange',
6 'fruit3' => 'grape',
7 'fruit4' => 'apple',
8 'fruit5' => 'apple');
9
10// this cycle echoes all associative array
11// key where value equals "apple"
12for($i = 0; $i< sizeof($array);$i++){
13if (key($array[$i]) == 'apple') {
14 echo key($array).'<br />';
15 }
16 //next($array);
17}
18?>
19
20
1
2<?php
3$array = array(
4 'fruit1' => 'apple',
5 'fruit2' => 'orange',
6 'fruit3' => 'grape',
7 'fruit4' => 'apple',
8 'fruit5' => 'apple');
9
10// this cycle echoes all associative array
11// key where value equals "apple"
12while ($fruit_name = current($array)) {
13 if ($fruit_name == 'apple') {
14 echo key($array).'<br />';
15 }
16 next($array);
17}
18?>
19
20