-1

I have this array i want to get the maximum id of an array, Is there any php function to find out the maximum value from array.For eg. in current example it should return 2 as highest value of column id.

Array
    (
        [0] => stdClass Object
            (
                [name] => 'p1'
                [id] => 0
            )
    
        [1] => stdClass Object
            (
                [name] => 'p2'
                [id] => 1
            )
    
        [2] => stdClass Object
            (
                [name] => 'p3'
                [id] => 2
            )
    )
user3653474
  • 3,393
  • 6
  • 49
  • 135
  • Use [`array_map()`](https://www.php.net/manual/en/function.array-map.php) to extract the value of the field `id` then [`max()`](https://www.php.net/manual/en/function.max.php) to find the largest value. – axiac Nov 02 '20 at 17:46
  • have a look here: https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields – Bret Weinraub Nov 02 '20 at 17:46
  • @BretWeinraub: This link is for sort i want to retrive largest value of column id – user3653474 Nov 02 '20 at 17:48

1 Answers1

0

Here take the ids using array_column and use max to get highest id

$arr = [
    [
    'name' => 'p1',
    'id' => 1
    ],
    'name' => 'p2',
    'id' => 2,
    [
    'name' => 'p1',
    'id' => 3
    ]
    ];

    $max_id = max( array_column( $arr, 'id'));
    var_dump( $max_id );
Al-Amin
  • 596
  • 3
  • 16