Sort an Array by keys based on another Array?

后端 未结 15 758
甜味超标
甜味超标 2020-11-22 03:29

Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing.

$custo         


        
相关标签:
15条回答
  • 2020-11-22 03:39

    I adopted the answer from @Darkwaltz4 for its brevity and would like to share how I adapted the solution to situations where the array may contain different keys for each iteration like so:

    Array[0] ...
    ['dob'] = '12/08/1986';
    ['some_key'] = 'some value';
    
    Array[1] ...
    ['dob'] = '12/08/1986';
    
    Array[2] ...
    ['dob'] = '12/08/1986';
    ['some_key'] = 'some other value';
    

    and maintained a "master key" like so:

    $master_key = array( 'dob' => ' ' ,  'some_key' => ' ' );
    

    array_merge would have executed the merge in the Array[1] iteration based on $master_key and produced ['some_key'] = '', an empty value, for that iteration. Hence, array_intersect_key was used to modify $master_key in each iterations like so:

    foreach ($customer as $customer) {
      $modified_key = array_intersect_key($master_key, $unordered_array);
      $properOrderedArray = array_merge($modified_key, $customer);
    }
    
    0 讨论(0)
  • 2020-11-22 03:40

    How about this solution

    $order = array(1,5,2,4,3,6);
    
    $array = array(
        1 => 'one',
        2 => 'two',
        3 => 'three',
        4 => 'four',
        5 => 'five',
        6 => 'six'
    );
    
    uksort($array, function($key1, $key2) use ($order) {
        return (array_search($key1, $order) > array_search($key2, $order));
    });
    
    0 讨论(0)
  • 2020-11-22 03:41

    There you go:

    function sortArrayByArray(array $array, array $orderArray) {
        $ordered = array();
        foreach ($orderArray as $key) {
            if (array_key_exists($key, $array)) {
                $ordered[$key] = $array[$key];
                unset($array[$key]);
            }
        }
        return $ordered + $array;
    }
    
    0 讨论(0)
  • 2020-11-22 03:42

    IF you have array in your array, you'll have to adapt the function by Eran a little bit...

    function sortArrayByArray($array,$orderArray) {
        $ordered = array();
        foreach($orderArray as $key => $value) {
            if(array_key_exists($key,$array)) {
                    $ordered[$key] = $array[$key];
                    unset($array[$key]);
            }
        }
        return $ordered + $array;
    }
    
    0 讨论(0)
  • 2020-11-22 03:44
    function sortArrayByArray(array $toSort, array $sortByValuesAsKeys)
    {
        $commonKeysInOrder = array_intersect_key(array_flip($sortByValuesAsKeys), $toSort);
        $commonKeysWithValue = array_intersect_key($toSort, $commonKeysInOrder);
        $sorted = array_merge($commonKeysInOrder, $commonKeysWithValue);
        return $sorted;
    }
    
    0 讨论(0)
  • 2020-11-22 03:45

    Another way for PHP >= 5.3.0:

    $customer['address'] = '123 fake st';
    $customer['name'] = 'Tim';
    $customer['dob'] = '12/08/1986';
    $customer['dontSortMe'] = 'this value doesnt need to be sorted';
    
    $customerSorted = array_replace(array_flip(array('name', 'dob', 'address')), $customer);
    

    Result:

    Array (
      [name] => Tim
      [dob] => 12/08/1986
      [address] => 123 fake st
      [dontSortMe] => this value doesnt need to be sorted
    )
    

    Works fine with string and numeric keys.

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