0

I want which is the biggest array from following array.

 [13] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 9
        )

[15] => Array
    (
        [0] => 1
        [1] => 5
        [2] => 8
    )

[33] => Array
    (
        [0] => 1
        [1] => 9
        [2] => 13
    )

I want a code that would return last array with key 33. Please Help.

4
  • max(array_keys($your_array)); Commented Mar 7, 2014 at 11:17
  • What do u mean by the biggest array? is it total sum? Commented Mar 7, 2014 at 11:18
  • @Nouphal M Compare all values from all array and return array which has highest value from all array.and it is not like that maximum key will come at last. Commented Mar 7, 2014 at 11:23
  • you mean 13 is the largest number so 33 index will be returned? Commented Mar 7, 2014 at 11:34

3 Answers 3

1

Use max to get the maximum from the keys of your array

$yourarray=array(13 => Array
(
    0 => 1,
            1 => 3,
            2 => 9,
        ),

15 => Array
(
    0 => 1,
        1 => 5,
        2 => 8,
    ),

33 => Array
(
    0 => 1,
        1 => 9,
        2 => 13,
    ));
$arr=max(array_keys($yourarray));
print_r($yourarray[$arr]);

Output:

Array
(
    [0] => 1
    [1] => 9
    [2] => 13
)
Sign up to request clarification or add additional context in comments.

2 Comments

Key is not always going to bigger at last.It is not like that array with max key is the highest one.
@user2645436 Check my answer it returning the value of the highest key in the array.
0

This should do the trick...

<?php

$tests = array(
    13 => array(1,3,9),
    15 => array(1,5,8),
    33 => array(1,9,13)
);

$array_totals = array_map('array_sum', $tests);
krsort($array_totals);
$maxArray = each($array_totals);

var_dump($maxArray);

Gives

array (size=4)
  1 => int 23
  'value' => int 23
  0 => int 33
  'key' => int 33

Comments

0

Not the most beautiful thing, but readable ;)

$tests = array(
    13 => array(1,3,9),
    15 => array(1,5,8),
    33 => array(1,9,13)
);

$max = -1;
$max_key = -1;
foreach ($tests as $k => $v) {
    $cur_max = max($v);
    if ($cur_max >= $max) {
        $max = $cur_max;
        $max_key = $k;
    }
}

echo "max: $max; max_key: $max_key";

Gives:

max: 13; max_key: 33

To make it more beautiful: use array_map and sorting.

1 Comment

Note to all: His question is a bit unclear, but what he wants is simply the index of the array that contains the biggest value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.