Home > Backend Development > PHP Tutorial > 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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:29:25
Original
1147 people have browsed it

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 of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template