Questions about replacing two-dimensional arrays in PHP

Source: Internet
Author: User
In PHP, two-dimensional array replacement problem A array: $ dbData = array (& nbsp; array (0, 'zhangsan', 'hangzhou'), & nbsp; array (1, 'Lee 4', 'Taobao'), & nbsp; array (2, 'two', 'two-dimensional array replacement in PHP
Array:
$ DbData = array (
Array (0, 'Zhang San', 'Dinner '),
Array (1, 'Li Si', 'Dinner '),
Array (2, 'two', 'hangzhou '),
Array (3, 'two', 'drinking water ')
);
B array
$ ExcelData = array (
Array (0, 'zhangsan', 'zhangsan '),
Array (1, 'lisi', 'Li si '),
Array (2, 'mazi', 'machin ')
);
To replace the elements in array A with array B, Michael Jacob uses zhangsan instead of lisi, and Ma zi uses mazi instead.
The following result is obtained:
C array:
$ DbData = array (
Array (0, 'hangsan ', 'hangzhou '),
Array (1, 'lisi', 'hangzhou '),
Array (2, 'mazi', 'hangzhou '),
Array (3, 'mazi', 'Shanghai ')
);

------ Solution --------------------
PHP code
$ DbData = array (0, 'Taobao', 'Taobao'), array (1, 'Li Si', 'Taobao'), array (2, 'machin ', 'hangzhou'), array (3, 'two', 'hangzhou'); $ excelData = array (0, 'hangsan', 'zhangsan '), array (1, 'lisi', 'Li si'), array (2, 'mazi', 'ma Zi'); // transform $ excelData because it is the replacement subject, therefore, the key-value pair must be a unique foreach ($ excelData as $ v) $ dict [$ v [2] = $ v; // start to replace foreach ($ dbData as & $ v) $ v [1] = $ dict [$ v [1] [1]; // check the result print_r ($ dbData );

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.