Saint Robson
Saint Robson

Reputation: 5525

How To Get ALL Minimum Value Within Multidimensional Array?

I have this code :

<?php

$result = array ( array ("split" => "2", "combo" => "4,3"),
                  array ("split" => "2", "combo" => "6,1"),
                  array ("split" => "3", "combo" => "2,1"),
                  array ("split" => "4", "combo" => "1,1,1,1"));

$min_x = min ( array_column( $result, 'split' ) );

print_r($min_x);
?>

this will give me 2 as result and I need to create some function to get the combo value of 2, how to get both array value of 2 using native PHP function (is there any?) like this :

array ( array ("split" => "2", "combo" => "4,3"),
        array ("split" => "2", "combo" => "6,1"));

Upvotes: 0

Views: 120

Answers (4)

Amit Rajput
Amit Rajput

Reputation: 2059

You can do this like below:

<?php

$result = array ( array ("split" => "2", "combo" => "4,3"),
                  array ("split" => "2", "combo" => "6,1"),
                  array ("split" => "3", "combo" => "2,1"),
                  array ("split" => "4", "combo" => "1,1,1,1"));

$min_x = min ( array_column( $result, 'split' ) );

$new_result = array();
foreach($result as $val){
    if($val["split"] == $min_x)
    $new_result[] = $val;
}
echo "<pre>";
print_r($new_result);

?>

Output :

Array
(
    [0] => Array
        (
            [split] => 2
            [combo] => 4,3
        )

    [1] => Array
        (
            [split] => 2
            [combo] => 6,1
        )

)

Upvotes: 1

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You can get the minimum number using min and array_column and finally simply use array_filter

$result = array ( array ("split" => "2", "combo" => "4,3"),
                  array ("split" => "2", "combo" => "6,1"),
                  array ("split" => "3", "combo" => "2,1"),
                  array ("split" => "4", "combo" => "1,1,1,1"));

$min = min(array_column($result,'split'));
$res = array_filter($result,function($v)use($min){ 
    return $v['split'] == $min;
});

print_r($res);

Output:

Array
(
    [0] => Array
        (
            [split] => 2
            [combo] => 4,3
        )

    [1] => Array
        (
            [split] => 2
            [combo] => 6,1
        )

)

Note : Working with PHP version >= 5.5.0

Upvotes: 4

Rogin Thomas
Rogin Thomas

Reputation: 772

YOu can try this,

<?php
$result = array ( array ("split" => "7", "combo" => "4,3"),
              array ("split" => "2", "combo" => "6,1"),
              array ("split" => "9", "combo" => "2,1"),
              array ("split" => "8", "combo" => "1,1,1,1"));
array_multisort($result);
print_r($result[0]['combo']);
?>

Upvotes: 1

Prakhar Singh
Prakhar Singh

Reputation: 206

$arr_min = array();
$arr_min["split"] = min ( array_column( $result, 'split' ) );
$arr_min["combo"] = min ( array_column( $result, 'combo' ) );

now you have an array of minimum value for both keys. this is just an abstract idea. You can also make it dynamic.

Upvotes: 1

Related Questions