带有子数组的PHP排序数组

时间:2017-09-22 14:03:13

标签: php

我有一个这样的数组:

Array(
  [0]=Array
  (
    [firstname] => john
    [lastname] => doe
    [age] => 35
    [salary] => Array
      (
        [gbp] => "180"
        [eur] => ""
        [usd] => ""
      )
  )
  [0]=Array
  (
    [firstname] => john
    [lastname] => doe
    [age] => 35
    [salary] => Array
      (
        [gbp] => ""
        [eur] => "180"
        [usd] => ""
      )
  )
  [0]=Array
  (
    [firstname] => john
    [lastname] => doe
    [age] => 35
    [salary] => Array
      (
        [gbp] => ""
        [eur] => ""
        [usd] => "224"
      )
  )
)

如何将最高工资开头的嵌套子阵列(按工资)的值排序到最低工资的子阵列,无论使用哪种货币?

3 个答案:

答案 0 :(得分:0)

您正在寻找的是PHP uasort http://php.net/manual/en/function.uasort.php

这将允许您创建所需的自定义排序条件。

答案 1 :(得分:0)

这里有一个帮助你的课程,有一个customValue()功能来提取你的数据并将其转换为通用货币(我用过' usd'),显然此转换每日更改

<?php
class ArrayCustomSort
{
    private $arrCustomValues = [];
    private $arrFieldOrder = [];

    public function setCustomValues(array $arrCustomValues)
    {
        $this->arrCustomValues = $arrCustomValues;
        return $this;
    }

    public function setFieldOrder(array $arrFieldOrder)
    {
        $this->arrFieldOrder = $arrFieldOrder;
        return $this;
    }

    private function customValue($field, $data)
    {
        $value = $data;
        if (is_object($data)) {
            $value = $data->$field;
        } elseif (is_array($data)) {
            $value = $data[$field];
        }

        $result = $value;
        if (array_key_exists($field, $this->arrCustomValues)) {
            $mxdOrder = $this->arrCustomValues[$field];
            if (is_array($mxdOrder) && ($result = array_search($value, $mxdOrder)) === false) {
                $result = count($mxdOrder);
            } elseif (is_string($mxdOrder) && function_exists($mxdOrder)) {
                $result = $mxdOrder($value);
            }
        }
        return $result;
    }

    private function cmp($a, $b)
    {
        $result = 0;
        reset($this->arrFieldOrder);
        while ($result === 0 && list($key, $value) = each($this->arrFieldOrder)) {
            if (is_int($key)) {
                $field = $value;
                $order = SORT_ASC;
            } else {
                $field = $key;
                $order = $value;
            }
            $order = (in_array($order, [SORT_ASC, SORT_DESC])) ? $order : SORT_ASC;

            $aValue = $this->customValue($field, $a);
            $bValue = $this->customValue($field, $b);
            if ($aValue > $bValue) {
                $result = 1;
            } elseif ($aValue < $bValue) {
                $result = -1;
            }
            if ($order === SORT_DESC) {
                $result *= -1;
            }
        }
        return $result;
    }

    public function arraySort(array &$arrData) {
        usort($arrData, ['self', 'cmp']);
        return $this;
    }
}

$arr = [
  [
    'firstname' => 'john',
    'lastname' => 'doe',
    'age' => 35,
    'salary' => [
        'gbp' => "180",
        'eur' => "",
        'usd' => ""
    ]
  ],
  [
    'firstname' => 'john',
    'lastname' => 'doe',
    'age' => 35,
    'salary' => [
        'gbp' => "",
        'eur' => "180",
        'usd' => ""
    ]
  ],
  [
    'firstname' => 'john',
    'lastname' => 'doe',
    'age' => 35,
    'salary' => [
        'gbp' => "",
        'eur' => "",
        'usd' => "224"
    ]
  ]
];

function customValue($value)
{
    static $arrToUSD = ['gbp' => 1.35, 'eur' => 1.19 , 'usd' => 1.00];
    $result = max([
        (!empty($value['gbp'])) ? $value['gbp'] * $arrToUSD['gbp'] : 0,
        (!empty($value['eur'])) ? $value['eur'] * $arrToUSD['eur'] : 0,
        (!empty($value['usd'])) ? $value['usd'] * $arrToUSD['usd'] : 0
    ]);
    return $result;
}

$objArrayCustomSort = new ArrayCustomSort();

$objArrayCustomSort->setCustomValues([
    'salary' => 'customValue'
]);

$objArrayCustomSort->setFieldOrder([
    'salary' => SORT_DESC
]);

$objArrayCustomSort->arraySort($arr);

var_dump($arr);

答案 2 :(得分:0)

首先,您需要遍历数组并根据货币分配值

$sortOrder = [];
foreach ($array as $person) {
    $sortOrder []= getCurrencyValue($person['salary']);
}

其中getCurrencyValue接受货币数组并返回int作为排序顺序。

使用http://php.net/manual/en/function.array-multisort.php

array_multisort($originalArray, $sortOrder)

这将按照与排序的$ sortOrder相同的顺序对原始数组进行排序。

有关详细信息,请参阅文档(查找与您正在进行的操作接近的卷和版本示例)