How to convert the following two-dimensional array to a one-dimensional array.
$msg = Array (
Array
' id ' = ' 45 ',
' Name ' = ' Jack '
),
Array
' id ' = ' 34 ',
' Name ' = ' Mary '
),
Array
' id ' = ' 78 ',
' Name ' = ' Lili '
),
);
1 Solution: foreach ($msg as $k = = $v) {
$ids [] = $id;
$names [] = $name;
}
2 Solution: $ids = Array_column ($msg, ' id ');
$names = Array_column ($msg, ' name ');
The above two methods Print_r ($names), the result is:
Array (
[0]=>jack
[1]=>mary
[2]=>lili
)
Note: Array_column (); can have a third parameter, such as $n = Array_column ($msg, ' name ', ' id ');
Print_r ($n); The result is:
Array (
[45]=>jack
[34]=>mary
[78]=>lili
)
Reference:array array_column ( array $array
, Mixed [ $column_key
, mixed $index_key
= null ])
http://www.bkjia.com/PHPjc/776510.html www.bkjia.com true http://www.bkjia.com/PHPjc/776510.html techarticle How to convert the following two-dimensional array to a one-dimensional array. $msg = array (' id ' = ' $ ', ' name ' = ' Jack '), array (' id ' = ' ' ", ' name ' = ' Mary '), array (' id ' = ' + ', ' name ' = ' Lili '),); 1 Solution: ...