How to convert the following two-dimensional array into a one-dimensional array.
Copy Code code as follows:
$msg = Array (
Array
' id ' => ' 45 ',
' Name ' => ' Jack '
),
Array
' id ' => ' 34 ',
' Name ' => ' Mary '
),
Array
' ID ' => ' 78 ',
' Name ' => ' Lili '
),
);
The first method:
Copy Code code as follows:
foreach ($msg as $k => $v) {
$ids [] = $id;
$names [] = $name;
}
The second method:
Copy Code code as follows:
$ids = Array_column ($msg, ' id ');
$names = Array_column ($msg, ' name ');
The above two solutions are Print_r ($names), and the following results are:
Copy Code code as follows:
Array (
[0]=>jack
[1]=>mary
[2]=>lili
)
Note: Array_column () can have a third parameter, such as $n = Array_column ($msg, ' name ', ' id ');
The results of Print_r ($n) are:
Copy Code code as follows:
Array (
[45]=>jack
[34]=>mary
[78]=>lili
)