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!