doukong5394
2018-04-12 20:29
浏览 44
已采纳

在PHP中任意对数组中的对象进行排序

I have a function that is looping over a bunch of items that each look something like this:

[
{"Target":"col-one","Value":0.261},
{"Target":"col-two","Value":0.881},
{"Target":"col-three","Value":0.571},
{"Target":"col-four","Value":0}
]

I would like to re-order each item so that instead of going in ascending order, it goes something like col-one, col-four, col-three, col-two. Is there a relatively simple method that can accomplish this type of arbitrary sorting?

To be clear, I want the result to look like the following:

[
     {"Target":"col-one","Value":0.261}, 
     {"Target":"col-four","Value":0}, 
     {"Target":"col-three","Value":0.571}, 
     {"Target":"col-two","Value":0.881}

]

图片转代码服务由CSDN问答提供 功能建议

我有一个循环遍历一堆项目的函数,每个项目看起来像这样: \ ñ

 <代码> [\ N { “目标”: “COL-一个”, “值”:0.261},\ N { “目标”: “COL-2”, “值”:0.881}  \ N { “目标”: “COL-3”, “值”:0.571},\ N { “目标”: “COL-4”, “值”:0} 
] 
上  
 
 

我想重新订购每个项目,以便不是按升序排列,而是类似col-one,col-four,col-three,col-two。 是否有一种相对简单的方法可以完成这种类型的任意排序?

要清楚,我希望结果如下所示:

  [
 {“Target”:“col-one”,“Value”:0.261},
 {“Target”:“col-four”,“Value”:0},
 {“Target  “:”col-three“,”Value“:0.571},
 {”Target“:”col-two“,”Value“:0.881} 
   
 
 

]

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongxian3852 2018-04-12 20:39
    已采纳

    If you have an array that defines the sort order, like:

    $order = ['col-one', 'col-four', 'col-three', 'col-two'];
    

    You can pass it into the usort comparison function and look up the sort index there. I think it's easier if you flip it so the text becomes the key.

    $order = array_flip($order);
    
    usort($items, function($a, $b) use ($order) {
        return $order[$a->Target] <=> $order[$b->Target];
    });
    

    If you don't flip it first, you have to array_search repeatedly instead. (Unless there's a better way I don't know of.)

    usort($items, function($a, $b) use ($order) {
        return array_search($a->Target, $order) <=> array_search($b->Target, $order);
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongqiabei7682 2018-04-12 20:36

    Two ways: sort the array using a custom callback function usort http://php.net/manual/en/function.usort.php , or write an array that's only the columns, then array_map to the new one: http://php.net/manual/en/function.array-map.php

    usort

    Make a new array that shows the column order you want.

    $columns = ['col-four', 'col-two', 'col-three', 'col-one'];
    

    Apply a sort function that compares the two column names by their index:

    usort($targets, function ($a, $b) use ($columns) {
        return array_search($a->Target], $columns) - array_search($b->Target], $columns);
    });
    

    So we're sorting, by comparing the difference in index.

    评论
    解决 无用
    打赏 举报
  • duanping1920 2018-04-12 20:40

    I think I understood your question , you want to sort your array in any order you want. You can use array_map

    $json='[
    {"Target":"col-one","Value":0.261},
    {"Target":"col-two","Value":0.881},
    {"Target":"col-three","Value":0.571},
    {"Target":"col-four","Value":0}
    ]';
    
    $order_to_sort=array('col-four','col-one','col-three','col-two');
    $arr=json_decode($json);
    
    function sortObjects($arr,$order_to_sort){
    return array_map(function ($a)use($arr) { return array_filter($arr, function($elem) use($a){
        return $elem->Target === $a;
    }); }, $order_to_sort);
    }
    echo "<pre>";
    print_r(sortObjects($arr,$order_to_sort));
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题