Sort an Array by keys based on another Array?

后端 未结 15 1009
甜味超标
甜味超标 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: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.

提交回复
热议问题