2
<?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]);
?>

I have the following code, and currently its outputting: Original Values

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 

It should be removing the array key [21] and [17] since they arethe max and min. then taking the average WITHOUT these two values and displaying it.

How do I get this to do this?

1

3 Answers 3

9
  1. sort the array (sort)
  2. remove the first element (array_shift)
  3. remove the last element (array_pop)
  4. create the sum of the remaining array (array_sum)
  5. divide the sum with the number of elements in the remaining array (count).

Code example:

sort($prices, SORT_NUMERIC);
array_shift($prices);
array_pop($prices);
$sum = array_sum($prices);
$average = $sum / count($prices);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the steps. How do I go about sorting based on value though. The us.php.net/sort link you gave me shows examples of sorting alphabetically, but not numerically.
You can use the SORT_NUMERIC flag for that. I added a code example that shows it.
Hakre, appreciated! Thank you.
4

You could use sort, array_shift, and array_pop

sort($prices); // Sort the array from lowest to highest value
array_pop($prices); // Remove the max value. Use $max = array_pop($prices); if you want to use the value
array_shift($prices); // Remove the min value. Use $min = array_shift($prices); if you want to use the value

echo array_sum($prices)/count($prices); // Echo the average without $max or $min

1 Comment

Where is array_average defined?
-1

You could also use array_sum, max, min and count

echo (array_sum($prices) - max($prices) - min($array)) / count($prices);

1 Comment

This answer is inaccurate/incorrect because the original count is used for the averaging (the min and max elements are not "removed").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.