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
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:29:20959browse

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:...
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