简体   繁体   中英

Find minimum and maximum key and value of an associative array php

May someone help me to find the minimum and maximum key and value for each year? I want for example to print "television-3" and "printer-1". This is the code in php

$year = array (
"year 2015 " =>  array( 
    "Television" => "3",
    "phone"     => "4",
    "personal computer"=> "5",
    "printer"     => "5",
),
"year 2016 " =>  array( 
    "Television" => "3",
    "phone"     => "7",
    "personal computer"=> "4",
    "printer"     => "1",
)
);
<?php
$year = array (
"year 2015 " =>  array( 
    "Television" => "3",
        "phone"     => "4",
            "personal computer"=> "5",
                "printer"     => "5",
                ),
                "year 2016 " =>  array( 
                    "Television" => "3",
                        "phone"     => "7",
                            "personal computer"=> "4",
                                "printer"     => "1",
                                ));
$result = []; 
foreach($year as $val)
{
    foreach($val as $k => $v) 
    {   
        $result[$k][] = $v; 
    }   
}
array_walk($result, function(&$v){
    $v = ['min' => min($v), 'max' => max($v) ];
});
var_dump($result);

output:

kris-roofe@krisroofe-Rev-station:~$ php cal.php 
array(4) {
  ["Television"]=>
  array(2) {
    ["min"]=>
    string(1) "3"
    ["max"]=>
    string(1) "3"
  }
  ["phone"]=>
  array(2) {
    ["min"]=>
    string(1) "4"
    ["max"]=>
    string(1) "7"
  }
  ["personal computer"]=>
  array(2) {
    ["min"]=>
    string(1) "4"
    ["max"]=>
    string(1) "5"
  }
  ["printer"]=>
  array(2) {
    ["min"]=>
    string(1) "1"
    ["max"]=>
    string(1) "5"
  }
}

I assume this is the code you're looking for:

function find_min_max_products($year) {
    foreach($year as $key => $value) {
        $year_str = explode(' ', trim($key));
        $year_str = end($year_str);

        $products_with_min_qty = array_keys($value, min($value));
        $products_with_min_qty = array_flip($products_with_min_qty);
        $products_with_min_qty = array_fill_keys(array_keys($products_with_min_qty), min($value));

        $products_with_max_qty = array_keys($value, max($value));
        $products_with_max_qty = array_flip($products_with_max_qty);
        $products_with_max_qty = array_fill_keys(array_keys($products_with_max_qty), min($value));  

        $products[$year_str] = array(
            'min' => $products_with_min_qty,
            'max' => $products_with_max_qty
        );
    }

    return $products;
}

$year = array (
    "year 2015 " =>  array( 
        "Television" => "3",
        "phone"     => "4",
        "personal computer"=> "5",
        "printer"     => "5",
    ),
    "year 2016 " =>  array( 
        "Television" => "3",
        "phone"     => "7",
        "personal computer"=> "4",
        "printer"     => "1",
    )
);

$products = find_min_max_products($year);
var_dump($products);

OUTPUT

Array
(
    [2015] => Array
        (
            [min] => Array
                (
                    [Television] => 3
                )

            [max] => Array
                (
                    [personal computer] => 3
                    [printer] => 3
                )

        )

    [2016] => Array
        (
            [min] => Array
                (
                    [printer] => 1
                )

            [max] => Array
                (
                    [phone] => 1
                )

        )

)

Hope it helps!

The solution using array_map , array_intersect , min , max and asort functions:

$result = array_map(function($y){
    $slice = array_intersect($y, [min($y), max($y)]);
    asort($slice);

    return $slice;    
}, $year);

// the min/max items are in ascending order (minimum values, then - maximun values)
print_r($result);

The output:

Array
(
    [year 2015 ] => Array
        (
            [Television] => 3
            [printer] => 5
            [personal computer] => 5
        )

    [year 2016 ] => Array
        (
            [printer] => 1
            [phone] => 7
        )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM