Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对数组最小值和最大值进行排序,但去掉初始的空字符串,并返回"&引用;所有值都相同的地方?_Php_Max_Min - Fatal编程技术网

Php 对数组最小值和最大值进行排序,但去掉初始的空字符串,并返回"&引用;所有值都相同的地方?

Php 对数组最小值和最大值进行排序,但去掉初始的空字符串,并返回"&引用;所有值都相同的地方?,php,max,min,Php,Max,Min,我试图获取一个数组的最小值和最大值,该数组按\u price、\u sale\u price和\u regular\u price排序,但将ID作为第一个键,我需要保持ID值与这些价格的关联。我正在使用下面的代码。。。但问题是它返回一个key=>value对给我,另一个问题是我得到min(“”)值返回的min(_price)值,但我需要它返回组中的最小正数 function GetMinMaxPrices(Array $prices) { if (empty($prices)) retu

我试图获取一个数组的最小值和最大值,该数组按
\u price
\u sale\u price
\u regular\u price
排序,但将ID作为第一个键,我需要保持ID值与这些价格的关联。我正在使用下面的代码。。。但问题是它返回一个key=>value对给我,另一个问题是我得到
min(“”)值返回的
min(_price)
值,但我需要它返回组中的最小正数

function GetMinMaxPrices(Array $prices)
{
    if (empty($prices)) return array();

    // Get highest and lowest for all keys:  _regular_price, _sale_price, and _price.
    foreach($prices as $varBar => $price_info)
    {
        foreach($price_info as $type => $value)
        {
            switch($type)
            {
                CASE "_price":
                $_price[$varBar]['_price'] = array($varBar => $value);
                break;
                CASE "_sale_price":
                $_sale_price[$varBar]['_sale_price'] = array($varBar => $value);
                break;
                CASE "_regular_price":
                $_regular_price[$varBar]['_regular_price'] = array($varBar => $value);
                break;
            }
        }
    }

    return array(
        '_price' => array(
            'max' => max(array_map("max", $_price)),
            'min' => min(array_map("min", $_price))
        ),
        '_sale_price' => array(
            'max' => max(array_map("max", $_sale_price)),
            'min' => min(array_map("min", $_sale_price))
        ),
        '_regular_price' => array(
            'max' => max(array_map("max", $_regular_price)),
            'min' => min(array_map("min", $_regular_price))
        )
    );
}



$newArray[234290911]['_price'] = '2';
$newArray[234230495]['_price'] = '35';
$newArray[239402343]['_price'] = '';

$newArray[239402343]['_regular_price'] = 65;
$newArray[234290911]['_regular_price'] = 70;
$newArray[234230495]['_regular_price'] = 35;

$newArray[234290911]['_sale_price'] = 1;
$newArray[239402343]['_sale_price'] = 1;
$newArray[234230495]['_sale_price'] = 10;

$newPrices = GetMinMaxPrices($newArray);

var_dump($newPrices);
在某些情况下,可能会在某些价格中发现空字符串,在这种情况下,它需要跳过它。如果给定组的所有值(
\u price
\u sale\u price
、或
\u regular\u price
)都是空字符串,则它需要为该组返回空字符串(与min当前一样),但前提是该组中的所有值都是空字符串。当前,如果组中的任何值是空字符串,则将其作为
min
值返回

我知道
min
就是为了做到这一点而设计的,但是如何解决这个问题,同时仍然获得$varBar(变体ID)关联

GetMinMaxPrices()函数的返回值所需的数组分别为:

array('_price' => array(
    'max_value' => $theMaxPriceValue,
    'max_id' => $varBar_max_id,
    'min_value' => $theMinPriceValue,
    'min_id' => $varBar_min_id
), // etc. etc. );
如果这些值都相同,我就不需要它了。如果最小值为“”,则我需要大于0的实际最小值。如果数组中每个组的所有值都等于一个空字符串,那么它应该返回一个空字符串,并且只返回一个空字符串

似乎我在函数中非常接近这一点,但就是不能让
min
返回一个非空字符串的值,例如,
\u price
数组中有一个空字符串,并给我一个
min
值“”,但应该给我一个
min
2

这是:

function GetMinMaxPrices(Array $prices)
{
    if (empty($prices)) { return array(); }
    else { $return_arr = array(); }

    // Get highest and lowest for all keys:  _regular_price, _sale_price, and _price.
    foreach($prices as $varBar => $price_info)
    {
      if ( !('' === $price_info['_price'] 
        && '' === $price_info['_regular_price'] 
        && '' === $price_info['_sale_price']) ) 
      {
        foreach($price_info as $type => $value)
        {
          if ( '' !== $value ) {
            switch($type)
            {
                CASE "_price":
                  if ( !isset($return_arr['_price']['max_value']) 
                    || $value > $return_arr['_price']['max_value'] ) {
                    $return_arr['_price']['max_value'] = $value;
                    $return_arr['_price']['max_id'] = $varBar;
                  }
                  if ( !isset($return_arr['_price']['min_value']) 
                    || $value < $return_arr['_price']['min_value'] ) {
                    $return_arr['_price']['min_value'] = $value;
                    $return_arr['_price']['min_id'] = $varBar;
                  }
                  break;
                CASE "_sale_price":
                  if ( !isset($return_arr['_sale_price']['max_value']) 
                    || $value > $return_arr['_sale_price']['max_value'] ) {
                    $return_arr['_sale_price']['max_value'] = $value;
                    $return_arr['_sale_price']['max_id'] = $varBar;
                  }
                  if ( !isset($return_arr['_sale_price']['min_value']) 
                    || $value < $return_arr['_sale_price']['min_value'] ) {
                    $return_arr['_sale_price']['min_value'] = $value;
                    $return_arr['_sale_price']['min_id'] = $varBar;
                  }
                  break;
                CASE "_regular_price":
                  if ( !isset($return_arr['_regular_price']['max_value']) 
                || $value > $return_arr['_regular_price']['max_value'] ) {
                    $return_arr['_regular_price']['max_value'] = $value;
                    $return_arr['_regular_price']['max_id'] = $varBar;
                  }
                  if ( !isset($return_arr['_regular_price']['min_value']) 
                    || $value < $return_arr['_regular_price']['min_value'] ) {
                    $return_arr['_regular_price']['min_value'] = $value;
                    $return_arr['_regular_price']['min_id'] = $varBar;
                  }
                  break;
            }
          }
        }
      }
    }
    return $return_arr;
}



$newArray[234290911]['_price'] = '2';
$newArray[234230495]['_price'] = '35';
$newArray[239402343]['_price'] = '';

$newArray[239402343]['_regular_price'] = 65;
$newArray[234290911]['_regular_price'] = 70;
$newArray[234230495]['_regular_price'] = 35;

$newArray[234290911]['_sale_price'] = 1;
$newArray[239402343]['_sale_price'] = 1;
$newArray[234230495]['_sale_price'] = 10;

$newPrices = GetMinMaxPrices($newArray);

print '<pre>';
print_r($newPrices);
print '</pre>';

您好,我正在获取未定义的变量<代码>$return\u arr
在第18行,
\u sale\u price
在第27行,以及
\u regular\u price
在第36行……嘿,谢谢,看起来不错,但是有没有办法消除错误?现在在线上。。。18、29和40。我的行似乎与你的行不一致,而且我没有收到错误,所以请描述您收到错误的行。另外,我在这里执行这段代码:不幸的是,我看不到共享它的方法。一个问题,您是否运行与示例中相同的$newArray?我正在这里运行您的确切代码:如果您在那里运行它,我只是将您的
print
函数更改为
var\u dump($newPrices)
明白了。关于消失和修复$return\u arr错误的警告。
Array
(
    [_price] => Array
        (
            [max_value] => 35
            [max_id] => 234230495
            [min_value] => 2
            [min_id] => 234290911
        )

    [_regular_price] => Array
        (
            [max_value] => 70
            [max_id] => 234290911
            [min_value] => 35
            [min_id] => 234230495
        )

    [_sale_price] => Array
        (
            [max_value] => 10
            [max_id] => 234230495
            [min_value] => 1
            [min_id] => 234290911
        )

)