Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

/**
 * Using array_merge() to reorder variables keys.
 */

// items in the wrong order.
$variables = array(
  'item 3' => 'value 3',
  'item 2' => 'value 2',
  'item 1' => 'value 1',
);

// Set keys in the correct order. Item values will be overwritten as long as
// all the keys from $variables have a match.
$reorder = array(
  'item 1' => NULL,
  'item 2' => NULL,
  'item 3' => NULL,
);

// The order in $variables will be set according to how $reorder set them.
$variables = array_merge($reorder, $variables);

print var_dump($variables);
// result:
// array(3) {
//   ["item 1"]=>
//   string(7) "value 1"
//   ["item 2"]=>
//   string(7) "value 2"
//   ["item 3"]=>
//   string(7) "value 3"
// }