Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hightest value of an associative array

Tags:

arrays

php

Is there any easy way to get the hightest numeric value of an associative array?

$array = array(
    0 => array(
        'key1' => '123',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    ),
    1 => array(
        'key1' => '124',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    ),
    2 => array(
        'key1' => '125',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    )
);

AwesomeFunction($array, 'key1'); // returns 2 ($array key)

Please be kind since this question was written with a phone. Thanks.

like image 440
Shoe Avatar asked Feb 23 '11 15:02

Shoe


People also ask

What is associative array example with example?

For example, the following statement defines an associative array a with key signature [ int, string ] and stores the integer value 456 in a location named by the tuple [ 123, "hello" ]: a[123, "hello"] = 456; The type of each object contained in the array is also fixed for all elements in a given array.

How do you find an associative array?

If count(filtered_array) == count(original_array), then it is an assoc array. If count(filtered_array) == 0, then it is an indexed array.

What is the maximum size of an array in PHP?

There is no max on the limit of an array. There is a limit on the amount of memory your script can use. This can be changed in the 'memory_limit' in your php.


1 Answers

PHP 5.5 introduced array_column() which makes this much simpler:

echo max(array_column($array, 'key1'));

Demo

like image 170
John Conde Avatar answered Oct 21 '22 10:10

John Conde