PHP is quick and easy to extract a column value of an element in a two-dimensional array as the key of the element in this two-dimensional array

Source: Internet
Author: User

Powerful built-in array functions thanks to PHP
Array_column ();
Array_combine ();

Give me a little chestnut:

<?PHP//Check out the basic information of the user first$userArray= [' id ' = ' = ' Zs ', ' name ' = = ' Zhang San '], [' id ' = ' ls ', ' name ' = ' John Doe '], [' id ' = ' wr ', ' name ' = ' King ']];//make a new user array of the values of a specific column within a two-dimensional array$userArrayNew= Array_column ($userArray, ' ID ');//merges the new user array with the fractional array (gets a new array of fractions) and takes the elements of the first array as the key of the second array element$userArrayCombine=Array_combine($userArrayNew,$userArray);Echo"Re-assembled user array".Php_eol;Var_dump($userArrayCombine);//query out score scores based on ID (in)$scoreArray= [' id ' = ' zs ', ' score ' + 98], [' id ' = ' ls ', ' score ' + ', ' ' id ' = ' wr ', ' score ' + 99]];//Query the list to return, and to take the name on theforeach($scoreArray  as&$v) {    if(!Empty($userArrayCombine[$v[' ID ']] [' Name '])) {        $v[' name '] =$userArrayCombine[$v[' ID ']] [' Name ']; } Else {        $v[' name '] = '; }}Echo"Re-assembled fractional array".Php_eol;Var_dump($scoreArray);

PHP is quick and easy to extract a column value of an element in a two-dimensional array as the key of the element in this two-dimensional array

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.