2 ways to convert two-dimensional arrays to one-dimensional arrays _php tutorial

Source: Internet
Author: User
How to convert the following two-dimensional array to a one-dimensional array.

Copy the Code code as follows:
$msg = Array (
Array
' id ' = ' 45 ',
' Name ' = ' Jack '
),
Array
' id ' = ' 34 ',
' Name ' = ' Mary '
),
Array
' id ' = ' 78 ',
' Name ' = ' Lili '
),
);

The first method:

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

The second method:

Copy the Code code as follows:
$ids = Array_column ($msg, ' id ');
$names = Array_column ($msg, ' name ');

The above two methods Print_r ($names), the result is:

Copy the Code code 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 ');

Print_r ($n); The result is:

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

http://www.bkjia.com/PHPjc/776761.html www.bkjia.com true http://www.bkjia.com/PHPjc/776761.html techarticle How to convert the following two-dimensional array to a one-dimensional array. Copy the code as follows: $msg = array (' id ' = ' * ', ' name ' = ' Jack '), array (' id ' = ' {') ', ' name ' = ' Mary '), array (' id ' = ' + ',...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.