Search for Highest Key/Index in an Array

Search for highest key/index in an array

This should work fine

$arr = array( 1 => "A", 10 => "B", 5 => "C" );
max(array_keys($arr));

Return index of highest value in an array

My solution is:

$maxs = array_keys($array, max($array))

Note:
this way you can retrieve every key related to a given max value.

If you are interested only in one key among all simply use $maxs[0]

Get the highest key in array less than 'x'

You can use array_filter() to get the array with keys less than 38, and the use max().

PHP 7.4+

$arr = ['10' => 'a', '20' => 'b', '30' => 'c', '40' => 'd', '50' => 'e'];
$threshold = 38;

// Keep elements under condition:
$limit = array_filter($arr, fn($key) => $key < $threshold, ARRAY_FILTER_USE_KEY);

$max = max(array_keys($limit)); // get the max of remain keys.
var_dump($max);

Outputs:

int(30)

Before PHP 7.4

$threshold = 38;
$arr = ['10' => 'a', '20' => 'b', '30' => 'c', '40' => 'd', '50' => 'e'];

$filtered = array_filter($arr, function($key) use ($threshold) { return $key < $threshold; }, ARRAY_FILTER_USE_KEY);
$max = max(array_keys($filtered));
var_dump($max); // int(30)

Get key of max element in array

Based on https://stackoverflow.com/a/1461363/1641835:

$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
$max_keys = array_keys($someArray, max($someArray));
// $max_keys would now be an array: [ 'ok' ]

$max_keys will now be an array of all the keys that point to the maximum value. If you know there will only be one, or you don't care which you retrieve, you could instead use:

$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
$max_key = array_search(max($someArray), $someArray);
// $max_key would now be 'ok'

Return index of greatest value in an array

This is probably the best way, since it’s reliable and works on old browsers:

function indexOfMax(arr) {
if (arr.length === 0) {
return -1;
}

var max = arr[0];
var maxIndex = 0;

for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
maxIndex = i;
max = arr[i];
}
}

return maxIndex;
}

There’s also this one-liner:

let i = arr.indexOf(Math.max(...arr));

It performs twice as many comparisons as necessary and will throw a RangeError on large arrays, though. I’d stick to the function.

PHP Find the Index of the highest key value

You can use a few of the array_... functions. First array_column() to extract the column used for the maximum values, then array_keys() to return all of the keys that match the maximum value (found using max($prices))...

$data = [
0 => [
"name" => "Nola - Roman Road",
"rating" => 4.2,
"price_level" => 3
],
1 => [
"name" => "The Camel",
"rating" => 4.6,
"price_level" => 4
],
2 => [
"name" => "The Dundee Arms",
"rating" => 4,
"price_level" => 2
]];

$rating = array_column($data, "rating");
$index = array_keys($rating, max($rating));

print_r($index);

This prints out ...

Array
(
[0] => 1
)

This will also work if several entries match the maximum value which may help.

Get highest index key of Javascript array

I am unsure why your code is not working, .length on an array like that shows 5 correctly in my example here

However, if you do not set values at all on specific indexes, you can do this :

var foo = [];
foo[1] = "bar";
foo[4] = "bar";

//grab existing indexes that have values assigned
var indexes = foo.map(function(val, idx) { return idx; });
//get the last one, this + 1 is your real length
var realLength = indexes[indexes.length - 1] + 1;
console.log("Real length: ", realLength);
//iterate using for loop
for(var i=0; i<realLength; i++) {
var val = foo[i];
console.log(i, val);
}

Search the key of the highest value and specific value

Use array.reduce to get the highest index.

Also, as noted by another user, the correct answer based on your description is 4, not 5 like you said.

var arr = [{
"value": "AHAH",
"field": "15",
"color": "",
"bug_tab": true,
"sequence": "40",
"text": "slash"
},
{
"value": "BABA",
"field": "8",
"color": "",
"bug_tab": true,
"sequence": "50",
"text": "zip"
},
{
"value": "CACA",
"field": "25",
"color": "",
"bug_tab": false,
"sequence": "63",
"text": "vite"
},
{
"value": "DADA",
"field": "22",
"color": "",
"bug_tab": true,
"sequence": "66",
"text": "meat"
},
{
"value": "EVA",
"field": "13",
"color": "",
"bug_tab": true,
"sequence": "70",
"text": "zut"
},
{
"value": "FAFA",
"field": "jut",
"color": "",
"bug_tab": false,
"sequence": "90",
"text": "cut"
}
];

var res = arr.reduce((acc, curr, idx, arr) =>
curr.bug_tab && +curr.sequence > +arr[acc].sequence ? idx : acc
, 0);

console.log(res);

Getting an array key using the max() function

array_search function would help you.

$returnThis = array_search(max($arrCompare),$arrCompare);


Related Topics



Leave a reply



Submit