0

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"));
0

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

1
$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.

Comments

1

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']);
?>

1 Comment

your code doesn't have duplicate value of split. that's why your code only give 1 value of lowest split, what I need is both value of lowest split when I have duplicate value of lowest split.
1

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
        )

)

1 Comment

I got this error : Warning: in_array() expects parameter 2 to be array, string given in

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.