2016-12-09 160 views
1

有人可以幫我找到每年的最小和最大鍵和值嗎? 我想要例如打印「電視-3」和「打印機-1」。 這是在PHP代碼查找關聯數組的最小和最大鍵和值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", 
) 
); 
+1

什麼應該是第一年的最大鍵/值? – RomanPerekhrest

+0

@RomanPerekhrest它應該是個人電腦-5和打印機-5 – bsh

+0

我想,你不會比較一些標準的關鍵,你只是想獲得最大/最小值和他們的鑰匙 – RomanPerekhrest

回答

0

使用array_maparray_intersectminmaxasort功能的解決方案:

$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); 

輸出:

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

    [year 2016 ] => Array 
     (
      [printer] => 1 
      [phone] => 7 
     ) 
) 
0
<?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); 

輸出:

[email protected]:~$ 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" 
    } 
} 
0

我想這是你要找的代碼:

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

輸出

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

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

     ) 

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

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

     ) 

) 

希望它有幫助!

相關問題