Code implementation of vertical merging & horizontal merging of two-dimensional arrays in PHP

Source: Internet
Author: User
This article brings the content is about PHP two-dimensional array of vertical merge & horizontal Merge code implementation, there is a certain reference value, the need for friends can refer to, I hope to help you.

This article is mainly to share with you the first way of merging: Using the Array_merge method given by the PHP array API to merge an array, we hope to help everyone.

Code:

$a = Array (Array ("1", "2"), Array ("3", "4"), $b = Array ("A", "B"), Array ("C", "D"));          $c = Array_merge ($a, $b);p Rint_r ($c);

Results:

This method of merging becomes a B array appended to the a array.

The second method of merging:

Merging arrays with a foreach loop

Code:

<?php$a = Array (Array ("1", "2"), Array ("3", "4"), $b = Array ("A", "B"), Array ("C", "D"));  foreach ($a as $key = + $vo) {$list [] = Array_merge ($vo, $b [$key]);} print_r ($list);

Results:

The method is then merged into a B array inserted into the a array.

Related Article

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.