从阵列中删除最大值/最小值,取平均值,输出平均值

Yo *_*Bro 1 php arrays indexing

<?php 
$url = 'https://www.googleapis.com/shopping/search/v1/public/products?key=thekey&country=US&q=nintendo+wii';

$data = curl_init($url);
curl_setopt($data, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($data, CURLOPT_HEADER, 0);

$product_result = curl_exec($data);
curl_close($data);


$arr = json_decode($product_result, true);
$prices = array();

echo "Original Values";

foreach ($arr['items'] as $item)
{
    if (isset($item['product']['inventories'][0]['price']))
    {
        $prices[] = $item['product']['inventories'][0]['price'];
    }
}


echo '<pre>';
print_r($prices);
echo '<br /><br />';

// Get the values
$min = $prices[$minIndex];
$max = $prices[$maxIndex];

// Find the values
$minIndex = array_search($min, $prices);
$maxIndex = array_search($max, $prices);

#print out results with no min/max pair
echo $prices[$min] . 'max is ' . $prices[$max];
echo '</pre>';
// Unset the values
unset($prices[$minIndex], $prices[$maxIndex]);
?>
Run Code Online (Sandbox Code Playgroud)

我有以下代码,目前正在输出:原始值

Array
(
    [0] => 199.95
    [1] => 149.99
    [2] => 129.99
    [3] => 149.99
    [4] => 149.99
    [5] => 124.99
    [6] => 225.99
    [7] => 149.96
    [8] => 149.99
    [9] => 149.99
    [10] => 184.99
    [11] => 199.99
    [12] => 184.98
    [13] => 149.99
    [14] => 326.99
    [15] => 269.96
    [16] => 348.95
    [17] => 108.9
    [18] => 149.99
    [19] => 149.99
    [20] => 229.99
    [21] => 357.38
    [22] => 169.96
    [23] => 229.98
    [24] => 187.99
)


max is 
Run Code Online (Sandbox Code Playgroud)

应该删除数组键[21]和[17],因为它们是最大值和最小值.然后取平均值而不用这两个值并显示它.

我怎么做到这一点?

hak*_*kre 6

  1. 排序数组(sort)
  2. 删除第一个元素(array_shift)
  3. 删除最后一个元素(array_pop)
  4. 创建剩余数组的总和(array_sum)
  5. 将总和除以剩余数组中的元素数(count).

代码示例:

sort($prices, SORT_NUMERIC);
array_shift($prices);
array_pop($prices);
$sum = array_sum($prices);
$average = $sum / count($prices);
Run Code Online (Sandbox Code Playgroud)