How to obtain the results of the corresponding array. Create a new array-php Tutorial

Source: Internet
Author: User
How to obtain the results of the corresponding array. Form a new corresponding Array. this post is finally edited by qq914260102 from 2013-10-3010: 58: 39 $ t1 & nbsp; Array & nbsp; (& nbsp; 0 & nbsp; & gt; & nbsp; Nanchang, & nbsp; 1 & nbsp; & gt; & nbsp; Nanchang, & nbsp; 2 & nbsp; & gt how to obtain the results of the corresponding array. Form a new corresponding array

This post was last edited by qq914260102 at 10:58:39, January 30 ,.

$ T1 = Array (0 => 'nanchang ', 1 => 'nanchang', 2 => 'ganzhou ', 3 => 'jiujiang', 4 => 'ganzhou ', 5 => 'jiujiang ');
$ T2 = Array (0 => 'excellent ', 1 => 'good', 2 => 'excellent ', 3 => 'good', 4 => 'excellent ', 5 => 'loan ');

For example, I have two arrays above. $ T1 [0] and $ t2 [0 ].... $ t1 [I] corresponds to $ t2 [I]. $ t1 [0] and $ t2 [0] represent ==== Nanchang, excellent.

I want to deduplicate $ t1 to form a new Array $ t11 = Array (0 => 'nanchang ', 1 => 'ganzhou', 2 => 'jiujiang ');
Split $ t2 at the same time. Form three new arrays (calculate the excellent, good, and poor values corresponding to the $ t11 array respectively ). Associated with $ t11.

Statistics on excellent quantity of Jiujiang in Nanchang and Ganzhou. (Calculate the number of excellent differences based on $ t2)
An excellent array is as follows:
$ T2_yx = Array (0 => '1', 1 => '2', 2 => '0 ');

A good array is as follows:
$ T2_lh = Array (0 => '1', 1 => '0', 2 => '1 ');
And other arrays are as follows:
$ T2_cd = Array (0 => '0', 1 => '0', 2 => '1 ');

What is the implementation of the program .. Thank you.

Share :? 'Nanchang ',? 1? =>? 'Nanchang ',? 2? =>? 'Ganzhou ',? 3? =>? 'Jiujiang ',? 4? =>? 'Ganzhou ',? 5? =>? 'Jiujiang ');? $ T2? = ?... 'Data-pics = ''>


------ Solution --------------------
// Step 1
$ T1 = Array (0 => 'nanchang ', 1 => 'nanchang', 2 => 'ganzhou ', 3 => 'jiujiang', 4 => 'ganzhou ', 5 => 'jiujiang ');
$ T2 = Array (0 => 'excellent ', 1 => 'good', 2 => 'excellent ', 3 => 'good', 4 => 'excellent ', 5 => 'loan ');
$ T = array_map (null, $ t1, $ t2 );
Print_r ($ t );
/* Get
Array
(
[0] => Array
(
[0] => Nanchang
[1] => excellent
)

[1] => Array
(
[0] => Nanchang
[1] => good
)

[2] => Array
(
[0] => Ganzhou
[1] => excellent
)

[3] => Array
(
[0] => Jiujiang
[1] => good
)

[4] => Array
(
[0] => Ganzhou
[1] => excellent
)

[5] => Array
(
[0] => Jiujiang
[1] => Poor
)
)
*/

// Step 2
$ R = array ();
Foreach ($ t as $ v ){
$ R [$ v [0] [] = $ v [1];
}
Print_r ($ r );
/* Get
Array
(
[Nanchang] => Array
(
[0] => excellent
[1] => good
)

[Ganzhou] => Array
(
[0] => excellent
[1] => excellent
)

[Jiujiang] => Array
(
[0] => good
[1] => Poor
)
)
*/

// Step 3
$ R = array_map ('Array _ count_values ', $ r );

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.