Reputation: 143
I have a array like this :
Array
(
[0] => 395
[1] => 0
[2] => 395
[3] => 395
[4] => 39
[5] => 17
[6] => 11
[7] => 35
[8] => 21
[9] => 11
[10] => 11
[11] => 0
[12] => 0
[13] => 0
[14] => 0
[15] => 0
[16] => 0
[17] => 375
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 22
[23] => 215
[24] => 215
[25] => 42
[26] => 163
[27] => 163
[28] => 61
[29] => 61
[30] => 134
[31] => 134
)
Now, i get the maximum Value of that array with this code :
echo max($similar);
For the array i said, the output will be : 395
that is in the array[0]
and array[2]
and array[3]
.
Now, i want to know How can i give this number (395) and get the location of that in the array ?
For example,
I need a function like this :
echo_value_from_num(395); // Output :: 0
The output is zero, mean first time 395 appeard in the [0] of array.
How we can get that number in the array with that Values ?
Upvotes: 0
Views: 45
Reputation: 4937
The function array_serach
(https://www.php.net/array_search) will return the key for a certain element.
In your example, it would be something like this:
$max = max($similar);
$key = array_search($max,$similar);
Upvotes: 2
Reputation: 1535
This will give you the locations (keys) with the highest values,
$max = array_keys($array, max($array));
Upvotes: 1