通过比较PHP中同一数组的元素值,使用用户定义的函数对关联数组进行排序

时间:2018-10-11 08:52:53

标签: php arrays sorting

我想通过匹配TAGS和ITEM_CD对数组元素进行排序。这是下面提到的源数组

array(
    array("TAGS" => "98056H","ITM_CD" => 51034),
    array("TAGS" => "98056H","ITM_CD" => 98056),
    array("TAGS" => "98056H","ITM_CD" => 84118),
    array("TAGS" => "96821H","ITM_CD" => 87591),
    array("TAGS" => "96821H","ITM_CD" => 96821)
);

我想要这样的输出

array(
    array("TAGS" => "98056H","ITM_CD" => 98056),
    array("TAGS" => "98056H","ITM_CD" => 51034),
    array("TAGS" => "98056H","ITM_CD" => 84118),
    array("TAGS" => "96821H","ITM_CD" => 96821)
    array("TAGS" => "96821H","ITM_CD" => 87591),
);

有人知道吗,请告诉我如何对这样的数组进行排序?。

在这里我提到了示例代码

function my_sort($a,$b) {
    if(str_replace('H','',$a['TAGS']) == $b['TAGS']){
        return 1;
    }
    return 0;
}
uasort($new_sort_arr,"my_sort");
print_r($new_sort_arr);

2 个答案:

答案 0 :(得分:0)

已更新

好的,我想我已经在这里解决了。

这将对“ TAGS”列进行排序,然后查找匹配的“ TAGS”和“ ITM_CD”值,并将它们冒泡到其各自的“ TAGS”组的顶部。

$array = array(

  array("TAGS" => '22222H',"ITM_CD" => 98056),
  array("TAGS" => '98056H',"ITM_CD" => 51034),
  array("TAGS" => '22222H',"ITM_CD" => 22222),
  array("TAGS" => '98056H',"ITM_CD" => 84118),
  array("TAGS" => '96821H',"ITM_CD" => 87591),
  array("TAGS" => '98056H',"ITM_CD" => 98056),
  array("TAGS" => '22222H',"ITM_CD" => 22222),
  array("TAGS" => '96821H',"ITM_CD" => 96821)

);


function order($v){

  if($v > 0){

    return 1;

  }elseif($v < 0){

    return -1;

  }else {

    return 0;

  }

}


usort($array, function($a, $b){
  //Store value of the string compare.
  $v = strcmp($a['TAGS'], $b['TAGS']);

  if($v == 0){ //If 0 then the "Tags" column is ok. Check the "ITM_CD" sort
    //This will sort matching "Tags" & "ITM_CD" to the top.
    return (str_replace('H','', $a['TAGS']) == $b['ITM_CD']) ? 0 : 1;

  }else{
    //The "Tags" column needs sorted.
    return order($v);

  }

});


echo '<pre>';
print_r(array_reverse($array));
echo '</pre>';

这将输出:

Array
(
    [0] => Array
        (
            [TAGS] => 98056H
            [ITM_CD] => 98056
        )

    [1] => Array
        (
            [TAGS] => 98056H
            [ITM_CD] => 84118
        )

    [2] => Array
        (
            [TAGS] => 98056H
            [ITM_CD] => 51034
        )

    [3] => Array
        (
            [TAGS] => 96821H
            [ITM_CD] => 96821
        )

    [4] => Array
        (
            [TAGS] => 96821H
            [ITM_CD] => 87591
        )

    [5] => Array
        (
            [TAGS] => 22222H
            [ITM_CD] => 22222
        )

    [6] => Array
        (
            [TAGS] => 22222H
            [ITM_CD] => 22222
        )

    [7] => Array
        (
            [TAGS] => 22222H
            [ITM_CD] => 98056
        )

)

答案 1 :(得分:0)

usort($new_sort_arr, function ($a, $b) {
    if ($o = $a['TAGS'] <=> $b['TAGS']) {
        // $o is not 0, meaning the two tags are different, so just return this value
        return $o;
    }

    // both TAGS are identical, sort within one TAG

    $aMatches = rtrim($a['TAGS'], 'H') == $a['ITM_CD'];
    $bMatches = rtrim($b['TAGS'], 'H') == $b['ITM_CD'];

    if ($aMatches == $bMatches) {
        // both match or both don't match, either way, they're equal
        return 0;
    } else if ($a) {
        // a matches, b doesn't, a is higher
        return -1;
    } else {
        // b matches, a doesn't, b is higher
        return 1;
    }
});