Php replaces the key value of an array with the rule of another array and obtains its value as a string.

Source: Internet
Author: User
Php replaces the key value of an array with the rule of another array and obtains the value to be converted into a string. now there are two arrays.
$ Arr1 = array ("q" => "1", "w" => "2", "e" => "3 "); $ arr2 = array ([0] => "You", [1] => "me", [2] => "him ");

Now I need to get an array like this
$ Arr = array ("q" => "You", "w" => "I", "e" => "He "); // then, you only need to get the value of this array and convert it into strings separated by commas $ str = 'You, me, him ';


Reply to discussion (solution)

$ Arr1 = array ("q" => "1", "w" => "2", "e" => "3 "); $ arr2 = array (0 => "You", 1 => "me", 2 => "He"); $ arr3 = array_combine (array_keys ($ arr1 ), $ arr2); print_r ($ arr3); echo join (',', $ arr3 );
Array ([q] => you [w] => I [e] => He) you, I, he

$ Arr1 = array ("q" => "1", "w" => "2", "e" => "3 "); $ arr2 = array (0 => "You", 1 => "me", 2 => "He"); $ arr3 = array_combine (array_keys ($ arr1 ), $ arr2); print_r ($ arr3); echo join (',', $ arr3 );
Array ([q] => you [w] => I [e] => He) you, I, he


Thank you, but I just tried it.
Let me give it to the instance.

Array ([0] => Array ([is_notice] => 5) [1] => Array ([is_notice] => 16 ))

This is an array of $ arr1, which is a parameter of the type. for example, the number 5, 16, ranging from 1 to 20, is read from the action database for initiating an application to cancel an application.
The number of records read based on the conditions may exceed 20, and there may be repeated records such as 5, 5, 6, 6, 10, and 10.

Then, $ arr2 indicates that the 20 types of Chinese descriptions from [0] to [19] are short words.

The message displayed to the user must be submitted by xxx in the following actions: initiate an application, reject the application, join, archive ,..... This way

Then, we tried echo join (',', $ arr3); value msgtype and the variable status = 1 to form a json string and return the following
{"Msgtype": null, "status": 1}

Print_r ($ arr3); null
Var_dump is bool (false)

$ Typearr = array (
[0] => "system Notification ",
[1] => "materials passed ",
[2] => "review failed ",
[3] => "Quota Update"
.....
);
This is the array of arr2. Please check if there are two arrays of arr1 arr2.



Array ([0] => Array ([is_notice] => 5) [1] => Array ([is_notice] => 16 ))
You are a two-dimensional array.

Array ([0] => Array ([is_notice] => 5) [1] => Array ([is_notice] => 16 ))
You are a two-dimensional array.



Ah, oh...
Reading only one field would also be an array
The is_notice field is read from the database.
How can I convert it
$ Arr1 = array (
"Is_notice" => "1 ",
"Is_notice" => "5 ",
"Is_notice" => "16"
);

Tutorial

Is that true?

$ Arr1 = array ('is _ notice '=> 3), array ('is _ notice' => 2 ), array ('is _ notice' => 1); $ arr2 = array (0 => 'system notification', 1 => 'material pass ', 2 => 'review failed', 3 => 'quota update'); $ arr3 = array (); foreach ($ arr1 as $ k => $ v) {$ arr3 [] ['is _ notice '] = $ arr2 [$ v ['is _ notice'];} print_r ($ arr3 );



Array
(
[0] => Array
(
[Is_notice] => quota update
)

[1] => Array
(
[Is_notice] => review failed
)

[2] => Array
(
[Is_notice] => material passed
)

)


Array ([0] => Array ([is_notice] => 5) [1] => Array ([is_notice] => 16 ))
You are a two-dimensional array.



Ah, oh...
Reading only one field would also be an array
The is_notice field is read from the database.
How can I convert it
$ Arr1 = array (
"Is_notice" => "1 ",
"Is_notice" => "5 ",
"Is_notice" => "16"
);

Tutorial


This is also a two-dimensional array.
In this way, you can
$ Arr3 [] = $ arr2 [$ v ['is _ notice '];

Thank you !!

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.