php how to find index of element in array value of which is less than number?

php get array value by key
php get array key by value multidimensional
get specific key value from array php
php array search multiple values
php search multidimensional array for multiple values
php search multidimensional array for value
php search array of objects
php get all values from multidimensional array

Say, there is an array and a number:

$values = ['a'=>10, 'b'=>20, 'c'=>30];
$num = 25;

How can I find an index of the array element the value of which is less than the number?

In the above example it will be the 'b' index of which is 1.

You could sort the array by the values then loop through it until a number is hit above the $num. Here is a quick example:

$values = ['a'=>10, 'b'=>20, 'c'=>30];
$num = 25;
$sortedValues = $values;
while (($current = current($sortedValues)) !== false && $current < $num) {
    $lastValue = key($sortedValues);
echo $lastValue;

array_search - Manual, array_search — Searches the array for a given value and returns the first is set to TRUE then the array_search() function will search for identical elements in the to find the correct index in an array containing an index with a value of 0. $​checked = ["1", "2", "3"]; // array with numbers in double quotes Example below: Return Value: Returns the key of a value if it is found in the array, and FALSE otherwise. If the value is found in the array more than once, the first matching key is returned. PHP Version: 4.0.5+. PHP Changelog: This function returns NULL if invalid parameters are passed to it (this applies to all PHP functions as of 5.3.0).

I got it:

$values = ['a'=>1, 'b'=>20, 'c'=>30, 'd'=>40, 'e'=>50];
$num = 55;
$i = null;
foreach ($values as $k => $v){
    if ($v == $num){
        echo $v;
    elseif ($num >= 1 && $v > $num){
        echo $values[$i];
    elseif ($num > end($values)){
        echo end($values);
    $i = $k;

array_values - Manual, array_values() returns all the values from the array and indexes the array numerically. See Also ¶ Doing this will cause PHP exceeds the momory limits: count the total number of times a specific value appears in array, use this function: in arrays (less than 500B per element in my case) which have many dimensions,  Return Values. Returns the key for needle if it is found in the array, FALSE otherwise. If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

May be array_filter can be some more handy:

$values = ['a'=>1, 'b'=>20, 'c'=>30, 'd'=>40, 'e'=>50];
$num = 45;

// filter items less than $num
$lesser = array_filter($values, function($v) use($num) { 
  return $v < $num;

// get necessary item/index from filtered, last for example
$lesserKeys = array_keys($lesser);
$lastOfLessKey = end($lesserKeys);
$lastOfLess = end($lesser);
var_dump($lastOfLessKey, $lastOfLess); // d 40

array_key_exists - Manual, array_key_exists — Checks if the given key or index exists in the array isset() - Determine if a variable is declared and is different than NULL Checks if a value exists in an array; property_exists() - Checks if the object or class has a property so you shouldn't warp the code into a single function like below, because the  Topic: PHP / MySQL Prev|Next. Answer: Use the PHP count() or sizeof() function. You can simply use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn't set.

    $values = ['a'=>10, 'b'=>20, 'c'=>30];
    $num = 25;

foreach($value as $key => $val){
    if( (int) $val == (int) $num){
         echo 'this is what you are searching. key is: '.$key.', value is: '.$val;


array_filter - Manual, array_filter — Filters elements of an array using a callback function Iterates over each value in the array passing them to the callback function. If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be function filter_by_value ($array, $index, $value){ Will return even numbers. Note: the Example 1, we use only two parameter. Because we can't mention `false` value. Because In default the in_array() take `false` as a boolean value. In above example, Example 1 : The `key1` is not value in the array. This is key of the array. So this scenario the in_array accept the search key as a value of the array.

array_count_values - Manual, Returns an associative array of values from array as keys and their count as value. Errors/Exceptions ¶. Throws E_WARNING for every element which is not string or integer. Simple way to find number of items with specific values in multidimensional array: <?php $list = [ Use $lower=true/false to ignore/set case Sensitiv. Given an array ‘a[]’ and number of queries q. Each query can be represented by l, r, x. Your task is to print the number of elements less than or equal to x in the subarray represented by l to r.

array_diff - Manual, Of course you can check deeper dimensions by using array_diff($array1[0], Computes the intersection of arrays with additional index check array_diff provides a handy way of deleting array elements by their value, The function returns true only if the two arrays contain the same number of values and each value in  Given an sorted array of size n. Find number of elements which are less than or equal to given element. Examples: Input : arr[] = {1, 2, 4, 5, 8, 10} key = 9 Output : 5 Elements less than or equal to 9 are 1, 2, 4, 5, 8 therefore result will be 5.

array_slice - Manual, The offset parameter denotes the position in the array, not the key. length If the array is shorter than the length , then only the available array elements will be present. 5.2.4, The default value of the length parameter was changed to NULL . $result_length = $before + 1 + $after; // Set the number of keys to return I have array with numeric values and I want to get key of first element which has value equal or greater than 5. Is there more elegant way than looping all elements in foreach? // "dirty" way fore