-1

I have a multi-dimension array :

array(3) {
    [0]=> array(3) {
              ["label"]=> string(2) "Ai"
              ["male"]=> int(0)
              ["female"]=> int(10) 
    }
    [1]=> array(3) {
              ["label"]=> string(2) "Bi"
              ["male"]=> int(0)
              ["female"]=> int(20) 
    }
    [2]=> array(3) {
              ["label"]=> string(2) "Ci"
              ["male"]=> int(10)
              ["female"]=> int(20) 
    }

I want to get all the same max value or all the same min value, I mean if there are 2 or more the same max or min value in the array, for example, I want to get all the same min value for male like this:

array(2) {
        [0]=> array(3) {
                  ["label"]=> string(2) "Ai"
                  ["male"]=> int(0)
        }
        [1]=> array(3) {
                  ["label"]=> string(2) "Bi"
                  ["male"]=> int(0)
        }

any solutions would be appreciated, thanks

Himawan Sandhi
  • 51
  • 1
  • 2
  • 6
  • 1
    _...get max and min value from that array.._ Value of which key? – B001ᛦ Jan 30 '20 at 11:42
  • 2
    what you mean with min and max? – Giacomo M Jan 30 '20 at 11:42
  • 2
    I don't see a coding attempt or a clear problem statement. – mickmackusa Jan 30 '20 at 12:09
  • @mickmackusa sorry for not putting a coding, because I used a common function that people often used for getting a max value or min value in the array, so that's why I thought people would be understood when I typed I had made a function to get max or min value – Himawan Sandhi Jan 31 '20 at 06:54
  • the main problem is if there are 2 min value in array, for example, 0 and 0, the output would be only 1 value, I want to get those 2 value, 0 and 0 in array or get all the same min value based on how many min values are in array – Himawan Sandhi Jan 31 '20 at 06:55
  • @GiacomoM I mean min or max value, sorry for the typo – Himawan Sandhi Jan 31 '20 at 06:55
  • The fundamental issue remains with your question -- it is a "requirements dump". In other words, your question has shown no effort to research or self solve; your question asks for volunteers to do your work for you, but Stackoverflow volunteers are NOT to be abused a free code writers. Think of code as cars; we are mechanics, not manufacturers. It is not too late to edit your question to show your failed attempt (assuming you tried something before asking). Is your input data the result of a database query? – mickmackusa Jan 31 '20 at 07:40
  • Solution equals https://stackoverflow.com/q/28372241/2943403 plus https://stackoverflow.com/q/1019076/2943403 – mickmackusa Jan 31 '20 at 07:48

2 Answers2

1

You can use a function with 3 input arguments $data array, max or min, gender male or female:

function getMinMaxGen($data,$minmax,$gen){

    $res = ['value' => 0,'sets' => []];  // default result

    // get min or max value
    if(in_array($minmax,['min','max'])){
        $arcol = array_column($data,$gen);      
        $res['value'] = $minmax($arcol);
    }

    // get sets of labels which has defined min or max value
    if(in_array($gen,['male','female'])){
        foreach($data as $person){
            if ($person[$gen] === $res['value']) $res['sets'][] = ['label' => $person['label'], "$gen" => $person[$gen]];
        }
    } 
    return $res;
}

Demo

Now you can retrieve any sets. Use $result['sets']

EDIT

Small code optimisation according to that post

Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • Please curb the urge to post answers to under-researched and unattempted questions. Even if this page was edited to become Clear/Complete, it is surely a duplicate. – mickmackusa Jan 30 '20 at 12:59
  • 1
    @mickmackusa, OP said **for example, I want to get min value for male**. I don't see anywhere here a link on dupe. Yes, it wasn't researched. I can't figure out why you thought it's unclear at all. – Aksen P Jan 30 '20 at 13:05
  • Read the requirements earlier in the same sentence. The OP made a function, but what exactly it does is ambiguous/ open to interpretation. Read the comments from the other users under the question -- I am not the onlyone who finds this question poorly described. **what if I wanted to get all** does this mean sex-irrespective? Or does it mean to keep the male/female separate and merely collect it all? – mickmackusa Jan 30 '20 at 13:08
  • Yeah, but for his case it is necessary to know `gender` and (`minmax` or `value from his function`). Why is it need to use two functions instead of the only one? Value possible to get in one row of code. Ok, you can manage more SO features than me that's why you'll be right even after all my words – Aksen P Jan 30 '20 at 13:17
  • We don't know "his case" -- it is too vague. I am not saying you shouldn't help people, but if volunteers answer questions that should be closed, then the quality of the questions will not improve over time. I am pleading to you because I cannot volunteer here 24/7, "regular/frequent volunteers need to work cooperatively to improve the content here, because we are outnumbered. – mickmackusa Jan 30 '20 at 13:20
  • @mickmackusa sorry but that's not a duplicate question, because I have researched before asking this question, maybe because I have an unclear question so you might think it's a duplicate question, but if so, give me the link for the duplicate question and I will delete this post soon. – Himawan Sandhi Jan 31 '20 at 06:59
  • @AksenP even some people don't understand my question but your answer has given a solution to my problem, thanks – Himawan Sandhi Jan 31 '20 at 07:01
  • @HimawanSandhi, sometimes happens such kind of situations, it's my third time. You're welcome – Aksen P Jan 31 '20 at 07:02
1

use array_column() as well as foreach() to get desired result:

$min =  min(array_column($array,'male'));//get min values from array for male

$keys = array_keys(array_column($array,'male'), $min); // get all index which have min values for male

$finalArray =[]; 
foreach($keys as $key){ // iterate over indexes
    $finalArray[] = array(
            'label'=>$array[$key]['label'],
            'male'=>$array[$key]['male']
        ); //assign values to new array

}

print_r($finalArray); //print Array

Output: https://3v4l.org/DSfFK

A functional approach to get all min max using different columns are like below:

function getMinMaxFromArray($array,$type,$columnToChoseForMinMax){

    if($type =='min'){
        $min =  min(array_column($array,$columnToChoseForMinMax));

        $keys = array_keys(array_column($array,$columnToChoseForMinMax), $min);

    }
    if($type =='max'){

        $max =  max(array_column($array,$columnToChoseForMinMax));

        $keys = array_keys(array_column($array,$columnToChoseForMinMax), $max);
    }
    $finalArray =[]; 
    foreach($keys as $key){
        $finalArray[] = array(
            'label'=>$array[$key]['label'],
            'male'=>$array[$key]['male']
        );

    }
    return $finalArray;
}

print_r(getMinMaxFromArray($array,'min','male'));
print_r(getMinMaxFromArray($array,'max','male'));

print_r(getMinMaxFromArray($array,'min','female'));
print_r(getMinMaxFromArray($array,'max','female'));

Output: https://3v4l.org/E38gV

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98