duancifu6769
2010-03-19 02:55 阅读 105
已采纳

array_multisort和动态变量选项

Im trying to sort any array with array_multisort and everything is working great. However, based on conditions in my script, I need to change the options. So What I have so far is this:

array_multisort(
 $sort1,SORT_ASC,
 $sort2,SORT_ASC,
 $sort3,SORT_ASC, 
 $arraytosort
);

and what I would like to have is something like this:

$dynamicSort = "$sort1,SORT_ASC,$sort2,SORT_ASC,$sort3,SORT_ASC,";

array_multisort(
 $dynamicSort, 
 $arraytosort
);

Any suggestions?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dousi8237 dousi8237 2010-03-19 03:23

    You could try to use call_user_func_array. But I've never tried it on a built-in function before. Here is an example:

    $dynamicSort = "$sort1,SORT_ASC,$sort2,SORT_ASC,$sort3,SORT_ASC";
    $param = array_merge(explode(",", $dynamicSort), array($arrayToSort))
    call_user_func_array('array_multisort', $param)
    
    点赞 评论 复制链接分享
  • duanluanhui8348 duanluanhui8348 2013-05-28 07:32

    I had the same problem with this answer: "Argument #1 is expected to be an array or a sort flag"

    For anyone having the same problem try this instead:

    $dynamicSort = array(&$sort1, SORT_ASC, &$sort2, SORT_ASC, &$sort3, SORT_ASC); 
    $param = array_merge($dynamicSort, array(&$arrayToSort));
    call_user_func_array('array_multisort', $param);
    

    Note that i have used the reference to my variables "&$" instead of $. This works great in php 5.3 but may cause error in 5.2 due to a bug.

    点赞 评论 复制链接分享
  • duanji1610 duanji1610 2014-07-11 15:16

    It is important to understand that the array sent to call_user_func_array() must consist only of references; it is not important whether the array itself is passed by reference. I spent the better part of a day troubleshooting this; the fact that the examples on the function page at php.net all used literal arrays led me to this page: php Bug #49353. Problem solved.

    This doesn't seem to be very well (or consistently) documented, so here goes....

    These DO NOT WORK (PHP 5.3.3):

    $multisort_array = array($arr1, SORT_DESC, SORT_STRING, $arr2);      // array of values
    call_user_func_array('array_multisort', $multisort_array);           // array passed by value
    
    $multisort_array = array($arr1, SORT_DESC, SORT_STRING, $arr2);      // array of values
    call_user_func_array('array_multisort', &$multisort_array);          // array passed by reference
    
    $multisort_array = array(&$arr1, SORT_DESC, SORT_STRING, &$arr2);    // non-constants by reference
    call_user_func_array('array_multisort', $multisort_array);           // array passed by value
    
    $multisort_array = array(&$arr1, SORT_DESC, SORT_STRING, &$arr2);    // non-constants by reference
    call_user_func_array('array_multisort', &$multisort_array);          // array passed by reference
    

    These DO WORK:

    $sort = array('desc' => SORT_DESC, 'string' => SORT_STRING);
    $multisort_array = array(&$arr1, &$sort['desc'], &$sort['string'], &$arr2);      // all by reference
    call_user_func_array('array_multisort', $multisort_array);                       // array passed by value
    
    $sort = array('desc' => SORT_DESC, 'string' => SORT_STRING);
    $multisort_array = array(&$arr1, &$sort['desc'], &$sort['string'], &$arr2);      // all by reference
    call_user_func_array('array_multisort', &$multisort_array);                      // array passed by reference
    
    点赞 评论 复制链接分享
  • dsovc00684 dsovc00684 2019-02-03 19:55

    To add onto the existing answers, just thought I would add a little something. For anyone passing the desired "sort by" as a comma-separated $_POST variable (or any comma-separated variable for that matter):

    //$_POST["sort_by"] = "column_A DESC, column_B ASC, columns_C DESC";
    $sort_bys = explode(",", $_POST["sort_by"]);
    $dynamicSort = array();
    foreach($sort_bys as $sort_by){
        $sort_by2 = trim(str_replace('DESC','',$sort_by));
        $direction = (strpos($sort_by, 'DESC') !== false)?SORT_DESC:SORT_ASC;
        $$sort_by2  = array_column($array_to_sort, $sort_by2);
        $dynamicSort[] = &$$sort_by2;
        $dynamicSort[] = $direction;
        $dynamicSort[] = SORT_NUMERIC; //or SORT_STRING or SORT_REGULAR ...
    }    
    $param = array_merge($dynamicSort, array(&$array_to_sort));
    call_user_func_array('array_multisort', $param);
    
    点赞 评论 复制链接分享

相关推荐