问题
Updated Code that creates array, but ISNT outputting the min/max values. Why isnt this working and how do I get it to automatically remove the min/max value and then reoutput the array?
<?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]);
?>
Current Output of the code that doesn't seem to show the min/max value:
Original Values
Array
(
[0] => 149.99
[1] => 149.99
[2] => 149.99
[3] => 209.95
[4] => 124.99
[5] => 225.99
[6] => 149.96
[7] => 249.99
[8] => 193.99
[9] => 149.99
[10] => 149.99
[11] => 149.99
[12] => 326.99
[13] => 269.96
[14] => 258.99
[15] => 129.99
[16] => 149.99
[17] => 39.99
[18] => 149.99
[19] => 209.99
[20] => 349.95
[21] => 357.38
[22] => 169.96
[23] => 125
[24] => 149.99
)
max is
回答1:
Based on unset
and array_search
from php.net, this should work:
// Find the values
$minIndex = array_search($minVal, $prices);
$maxIndex = array_search($maxVal, $prices);
// Get the values
$min = $prices[$minIndex];
$max = $prices[$maxIndex];
// Unset the values
unset($prices[$minIndex], $prices[$maxIndex]);
// Print out values
echo 'min value: '.$min;
echo 'max value: '.$max;
// Print full array
print_r($prices);
回答2:
You may sort the array in numeric order and then use array_pop() and array_shift()
sort($prices,SORT_NUMERIC);
$maxval = array_pop($prices);
$minval = array_shift($prices);
$avgofval = (count($prices))
? array_sum($prices)/count($prices)
: 0;
echo "min is ". $minval . "<br>Max is ". $maxval . "<br />avg is " . $avgofval ;
echo '<pre>'.print_r($prices,1).'</pre>';
回答3:
$minVal
and $maxVal
are not defined before the array search call.
// Find the values
$minIndex = array_search($minVal, $prices);
$maxIndex = array_search($maxVal, $prices);
And array_search doesn't find any value. It find the keys: Returns the key for needle if it is found in the array, FALSE otherwise.
$minIndex
is FALSE
, and $maxIndex
is also FALSE
.
The values are also incorrect...
// Get the values
$min = $prices[$minIndex]; // $min = $prices[FALSE]
$max = $prices[$maxIndex]; // $max = $prices[FALSE]
A possible solution is:
$minValue = FALSE;
$maxValue = 0;
foreach ($arr['items'] as $item) {
if (isset($item['product']['inventories'][0]['price']) !== false) {
$price = $item['product']['inventories'][0]['price'];
$prices[] = $price;
if ( ! $minValue || $minValue < $price) {
$minValue = $price;
}
if ($price > $maxValue) {
$maxValue = $price;
}
}
}
Aside
if (isset($item['product']['inventories'][0]['price']) !== false) { ... }
is a redundant test. It's exactly the same as
if (isset($item['product']['inventories'][0]['price'])) { ... }
来源:https://stackoverflow.com/questions/8468871/auto-delete-max-min-from-array-on-creation