从创建阵列自动删除最大值/最小值 [英] Auto-delete Max/Min from Array on creation

查看:145
本文介绍了从创建阵列自动删除最大值/最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新code创建阵列,但是,这不是输出的最大/最小值。 为什么心不是这方面的工作,我如何让它自动删除最小值/最大值,然后再次输出数组?

 < PHP
$ URL = 'https://www.googleapis.com/shopping/search/v1/public/products?key=thekey&country=US&q=nintendo+wii';$数据= curl_init($网址);
curl_setopt($的数据,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($数据,CURLOPT_HEADER,0);$ product_result = curl_exec($的数据);
curl_close($数据);
$ ARR = json_de code($ product_result,真正的);
$价格=阵列();回声原始值;的foreach($改编['项目']为$项)
{
    如果(使用isset($项目['产品'] ['库存'] [0] ['价格']))
    {
        $价格[] = $项目['产品'] ['库存'] [0] ['价格'];
    }
}
呼应'< pre>';
的print_r($价格);
呼应'< BR />< BR />';//获取值
$分= $价格[$ minIndex];
$最大= $价格[$ maxIndex];//查找值
$ minIndex = array_search($分钟,$的价格);
$ maxIndex = array_search($最大,$的价格);#PRINT出来,没有最小/最大对结果
回声$价格[$分钟。 最大的。 $价格[$ MAX];
呼应'< / pre>';
//取消设置值
未设置($价格[$ minIndex],$价格[$ maxIndex]);
?>

在code,它似乎并没有显示最小值/最大值的电流输出:

 原始值
排列

    [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为


解决方案

根据取消设置 array_search 从PHP .NET,这应该工作:

  //查找值
$ minIndex = array_search($ MINVAL,$的价格);
$ maxIndex = array_search($ MAXVAL,$的价格);//获取值
$分= $价格[$ minIndex];
$最大= $价格[$ maxIndex];//取消设置值
未设置($价格[$ minIndex],$价格[$ maxIndex]);//打印出值
回声最小值:$分钟。
呼应最大值:$最大。//打印全阵列
的print_r($价格);

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 

解决方案

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);

这篇关于从创建阵列自动删除最大值/最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆