比较数组中的项目而不丢失信息

时间:2016-06-30 23:28:01

标签: php arrays wordpress

我有一系列产品信息。我有一个循环遍历数组并比较价格以找到最低价格。

$rows = get_field('variations',$product_id);
   if($rows){
      $prices = array();
      foreach($rows as $row){
         $prices[] = $row['variation_price'];
      }
   //Getting the lowest price from the array
   $lowest_price = min($prices);
   }
   echo $lowest_price;

但是我现在需要获得与"最低价格相关的其他信息"数组。 IE浏览器。我需要最低价产品的ID等。

欢迎任何帮助!

这是数组的转储

Array

(     [0] =>排列         (             [] =>             [variation_title] =>小本生意             [checkout] =>大车             [trial] =>排列                 (                     [0] =>审讯                 )

        [variation_price] => 7
        [variation_subscription_cycle] => /month
        [variation_id] => 405
        [variation_url] => 
        [custom] => Array
            (
                [0] => Array
                    (
                        [custom_field] => 1 Domain
                    )

                [1] => Array
                    (
                        [custom_field] => 1 Million QPM
                    )

                [2] => Array
                    (
                        [custom_field] => 50 Records
                    )

                [3] => Array
                    (
                        [custom_field] => DNS Alias
                    )

            )

        [css_styles] => 
    )

[1] => Array
    (
        [] => 
        [variation_title] => Medium Business
        [checkout] => cart
        [trial] => Array
            (
                [0] => trial
            )

        [variation_price] => 35
        [variation_subscription_cycle] => /month
        [variation_id] => 286
        [variation_url] => 
        [custom] => Array
            (
                [0] => Array
                    (
                        [custom_field] => 10 Domains
                    )

                [1] => Array
                    (
                        [custom_field] => 5 Million QPM
                    )

                [2] => Array
                    (
                        [custom_field] => 500 Records
                    )

                [3] => Array
                    (
                        [custom_field] => DNS Alias
                    )

            )

        [css_styles] => ribbon
    )

[2] => Array
    (
        [] => 
        [variation_title] => Large Business
        [checkout] => cart
        [trial] => Array
            (
                [0] => trial
            )

        [variation_price] => 100
        [variation_subscription_cycle] => /month
        [variation_id] => 406
        [variation_url] => 
        [custom] => Array
            (
                [0] => Array
                    (
                        [custom_field] => 50 Domains
                    )

                [1] => Array
                    (
                        [custom_field] => 15 Million QPM
                    )

                [2] => Array
                    (
                        [custom_field] => 1,,500 Records
                    )

                [3] => Array
                    (
                        [custom_field] => DNS Alias
                    )

            )

        [css_styles] => 
    )

1 个答案:

答案 0 :(得分:0)

<?php

class Test {

public $a;
public $b;

    public function __construct($a1,$b1)
{
    $this->a = $a1;
    $this->b = $b1;
}
}

$first = new test(1,12);
$second = new test(4,25);
$third = new test(7,9);

$values = [$first, $second, $third];

function compare (Test $firstObject, Test $secondObject) {

return $firstObject->a > $secondObject->a;

}

usort($values, "compare");

    var_dump($values);

?>