更新了创建数组的代码,但ISNT输出了最小/最大值。 为什么这不起作用?如何让它自动删除最小/最大值然后重新输出数组?
<?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]);
?>
当前输出的代码似乎没有显示最小/最大值:
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
答案 0 :(得分:1)
基于来自php.net的unset
和array_search
,这应该有效:
// 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);
答案 1 :(得分:0)
您可以按数字顺序对数组进行排序,然后使用array_pop()和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>';
答案 2 :(得分:0)
$minVal
和$maxVal
。
// Find the values
$minIndex = array_search($minVal, $prices);
$maxIndex = array_search($maxVal, $prices);
并且array_search找不到任何值。它找到键:如果在数组中找到,则返回针的键,否则返回FALSE。
$minIndex
为FALSE
,$maxIndex
也为FALSE
。
值也不正确......
// Get the values
$min = $prices[$minIndex]; // $min = $prices[FALSE]
$max = $prices[$maxIndex]; // $max = $prices[FALSE]
可能的解决方案是:
$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;
}
}
}
除了
if (isset($item['product']['inventories'][0]['price']) !== false) { ... }
是一项冗余测试。它与
完全相同if (isset($item['product']['inventories'][0]['price'])) { ... }