Home  >  Article  >  Backend Development  >  2 ways to convert a two-dimensional array to a one-dimensional array_PHP tutorial

2 ways to convert a two-dimensional array to a one-dimensional array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:251102browse

How to convert the following two-dimensional array into a one-dimensional array.

Copy code The code is as follows:

$msg = array(
array(
>'id'=> '45',
'name'=>'jack'
),
array(
mary'
 ),
array(
) 'id'=>'78',
'name'=>'lili'
),
);

First method:

Copy code The code is as follows:
foreach($msg as $k => $v){
 $ ids[] = $id;
 $names[] = $name;
 }

Second method:

Copy code The code is as follows:
$ids = array_column($msg, 'id');
$names = array_column($msg, 'name');

The result of the above two solutions print_r($names); is:

Copy code The code is 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 result of

print_r($n); is:

Copy code The code is as follows:
Array(
 [45]=>jack
 [34] =>mary
 [78]=>lili
)

http://www.bkjia.com/PHPjc/776761.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/776761.htmlTechArticleHow to convert the following two-dimensional array into a one-dimensional array. Copy the code as follows: $msg = array( array( 'id'='45', 'name'='jack' ), array( 'id'='34', 'name'='mary' ), array( 'id'='78',...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn