Home > Backend Development > PHP Tutorial > Quick implementation: Convert a two-dimensional array to a one-dimensional array_PHP tutorial

Quick implementation: Convert a two-dimensional array to a one-dimensional array_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:29:20
Original
957 people have browsed it

How to convert the following two-dimensional array into 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 result of the above two solutions print_r($names); is:

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:

Array(

 [45]=>jack

 [34]=>mary

 [78]=>lili

)

Reference: array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/776510.htmlTechArticleHow to convert the following two-dimensional array into a one-dimensional array. $msg = array( array( 'id'='45', 'name'='jack' ), array( 'id'='34', 'name'='mary' ), array( 'id'=' 78', 'name'='lili' ), ); Solution 1:...
Related labels:
php
source:php.cn
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