This article to share the content is about the PHP array problem processing method, has a certain reference value, the need for friends can refer to
There are two sets of data in the one array, the value of the specified key as the new index, and the value of the other key as the value of the new index.
public static function index (array $array, $name) { $indexedArray = array (); if (empty ($array)) { return $indexedArray; } foreach ($array as $item) { if (isset ($item [$name])) {$indexedArray [$item [ $name]] = $item; Continue; } } return $indexedArray; }
For example: $array = array (' ID ' =>1,
' Name ' = ' Xiaogou '
),
Array (' ID ' =>2,
' Name ' = ' Xiaomao '
)
Direct return index ($array, ' id ');
The result is
Array (' 1 ' = ' Xiaogou ', ' 2 ' = ' Xiaomao ');
Gets the value collection of the array-specified key
public static function column (array $array, $columnName) { if (empty ($array)) { return array (); } $column = Array (); foreach ($array as $item) { if (isset ($item [$columnName])) { $column [] = $item [$columnName]; } } return $column; }
For example:
$array = array (' ID ' =>1, ' name ' = ' Xiaogou '), array (' ID ' =>2, ' name ' = ' Xiaomao ')); $array _ids = Column ($ Array, ' ID ');
$array _ids result is an array (0=>1,1=>2);
Third, get the value of the array specified key (unlike the above set, this is the direct fetch value)
public static function get (array $array, $key, $default) { if (isset ($array [$key])) { return $array [$key]; } else { return $default; }}
Iv. replacing values of the same key for a multidimensional array
public static function rename (array $array, array $map) { $keys = Array_keys ($map); foreach ($array as $key = + $value) { if (In_array ($key, $keys)) { $array [$map [$key]] = $value; Unset ($array [$key]); } return $array; }