Use array_multisort() with custom function

后端 未结 2 2027
南旧
南旧 2021-01-26 18:46

I have the following array and I would like to sort it according another array and not DESC or ASC

$array = array(
    \'note\' => a         


        
相关标签:
2条回答
  • 2021-01-26 19:19

    If you know the depth of the array you can simply apply usort on each array element to be sorted.

    Here is an example which orders according to a custom array:

    <?php
    $order = array(
        'first',
        'second',
        'third',
        'fourth',
        'fifth'
    );
    
    $array = array(
        array(
            'second',
            'fourth',
            'first',
            'third'
        ),
        array(
            'second',
            'first'
        )
    );
    
    foreach($array as &$value) {
        usort($value, function($a, $b) use($order) {
            return array_search($a, $order) > array_search($b, $order);
        });
    }
    unset($value);
    
    var_dump($array);
    /*
    array(2) {
      [0]=>
      array(4) {
        [0]=>
        string(5) "first"
        [1]=>
        string(6) "second"
        [2]=>
        string(5) "third"
        [3]=>
        string(6) "fourth"
      }
      [1]=>
      array(2) {
        [0]=>
        string(5) "first"
        [1]=>
        string(6) "second"
      }
    }
    */
    

    If you don't know how deep the array can go, the only solution that comes to my mind is a recursive function:

    <?php
    $order = array(
        'first',
        'second',
        'third',
        'fourth',
        'fifth'
    );
    
    $array = array(
        array(
            'second',
            'fourth',
            'first',
            'third'
        ),
        array(
            array('second', 'first'),
            array('fourth', 'third')
        )
    );
    
    function custom_multisort($array, $order) {
        foreach($array as &$value) {
            if(is_array($value[0])) {
                $value = custom_multisort($value, $order);
            } else {
                usort($value, function($a, $b) use($order) {
                    return array_search($a, $order) > array_search($b, $order);
                });
            }
        }
        return $array;
    }
    
    $array = custom_multisort($array, $order);
    
    var_dump($array);
    /*
    array(2) {
      [0]=>
      array(4) {
        [0]=>
        string(5) "first"
        [1]=>
        string(6) "second"
        [2]=>
        string(5) "third"
        [3]=>
        string(6) "fourth"
      }
      [1]=>
      array(2) {
        [0]=>
        array(2) {
          [0]=>
          string(5) "first"
          [1]=>
          string(6) "second"
        }
        [1]=>
        array(2) {
          [0]=>
          string(5) "third"
          [1]=>
          string(6) "fourth"
        }
      }
    }
    */
    
    0 讨论(0)
  • 2021-01-26 19:25

    I think its not possible. Instead of that do like this

    $custom_function_value = custom_function();
    array_multisort($array['type'], $array['year'], $custom_function_value, $array['note']);
    

    I think this will give your desired output.

    0 讨论(0)
提交回复
热议问题