4

Below is my array:

Array
(
    [3] => Array
        (
            [2] => 3
            [4] => 1
            [5] => 2
            [6] => 2
        )

    [5] => Array
        (
            [2] => 1
            [3] => 2
            [4] => 3
            [6] => 3
        )

In this array I want to find the maximum number and if the array contains the same maximum values then we choose one maximum value randomly.

Expected output like below:

Array
(
    [3] => Array
        (
            [2] => 3

        )

    [5] => Array
        (

            [4] => 3

        )

    [6] => Array
        (
            [2] => 3

        )

)

This is what I've tried:

$found = Array( [3] => Array ( [2] => 3 [4] => 1 [5] => 2 [6] => 2 ) [5] => Array ( [2] => 1 [3] => 2 [4] => 3 [6] => 3 ) [6] => Array ( [2] => 3 [3] => 2 [4] => 2 [5] => 3 )) 
$fmaxnum = array(); 

foreach($found as $fk => $fv){ 
   $fmaxnum[$fk] = max($fv); 
} 
echo "<pre>";print_r($fmaxnum);echo "</pre>"
5
  • I'm just going to leave this here Commented Mar 24, 2016 at 7:18
  • $found = Array ( [3] => Array ( [2] => 3 [4] => 1 [5] => 2 [6] => 2 ) [5] => Array ( [2] => 1 [3] => 2 [4] => 3 [6] => 3 ) [6] => Array ( [2] => 3 [3] => 2 [4] => 2 [5] => 3 ) ) $fmaxnum = array(); foreach($found as $fk => $fv){ $fmaxnum[$fk] = max($fv); } } echo "<pre>";print_r($fmaxnum);echo "</pre>"; Commented Mar 24, 2016 at 7:21
  • i write code like above Commented Mar 24, 2016 at 7:22
  • I've edited your comment into the code. If you want to update your question, use the "edit" link below your post. Also, to get something in code markdown, indent it by 4 spaces. Good luck! Commented Mar 24, 2016 at 7:25
  • do you just want the highest value no matter what key it have ? Commented Mar 24, 2016 at 7:50

3 Answers 3

3

You will get max value with max() but for index you have to use array_keys()

You can try this:

$found = Array ( '3' => Array ( '2' => 3 ,'4' => 1, '5' => 2, '6' => 2 ),
                 '5' => Array ( '2' => 1 ,'3' => 2, '4' => 3, '6' => 3 ),
                 '6' => Array ( '2' => 3 ,'3' => 2, '4' => 2, '5' => 3 )
                );
$fmaxnum = array(); 
foreach($found as $fk => $fv){ 
    $max_key = array_keys($fv, max($fv));
    $fmaxnum[$fk] = array(
      ($max_key[0]) => max($fv) /* This will give small index value */  
      /* (count($max_key)-1) => => max($fv) // this will give highest index value */
    );
} 

echo "<pre>";
print_r($fmaxnum);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

Comments

3

Simple solution with array_map and array_keys functions:

// supposing $arr is your initial array
$arrMax = array_map(function($v){
    $maximum = max($v);
    return  [array_keys($v, $maximum)[0] => $maximum];
}, $arr);

print_r($arrMax);

The output:

Array
(
    [3] => Array
        (
            [2] => 3
        )

    [5] => Array
        (
            [4] => 3
        )

    [6] => Array
        (
            [2] => 3
        )
)

Comments

0

if you just want to know the highest value as I understood

In this array I want to find the maximum number and if the array contains the same maximum values then we choose one maximum value randomly.

you could just do this

$array = [['3','1','2','2'],['1','2','3','3'],['2','1','1','3']]; 
$result = []; 

foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
    $result[] = $value;
}

echo max($result);  

this will foreach all the array and push the values on $result, then you just have one level array and easily use max function

Comments

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.