Skip to content

Instantly share code, notes, and snippets.

@guiassemany
Created November 29, 2016 18:38
Show Gist options
  • Select an option

  • Save guiassemany/6e353b7b7d614a98f4aef5818aa7dd66 to your computer and use it in GitHub Desktop.

Select an option

Save guiassemany/6e353b7b7d614a98f4aef5818aa7dd66 to your computer and use it in GitHub Desktop.
Sort array keys based on another array.
<?php
$unsorted = [
['d'=> 'teste', 'c' => 'teste2', 'e' => 'teste3'],
['d'=> 'teste', 'c' => 'teste2', 'e' => 'teste3'],
['d'=> 'teste', 'e' => 'teste3', 'c' => 'teste2', 'f' => 'valor']
];
$keys = ['c','e','d', 'f'];
$sorted = [];
foreach($unsorted as $key => $item){
$sorted[] = array_merge(array_flip($keys), $item);
}
var_dump($sorted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment