How to add the same key values of the two functions to form a new array.

Source: Internet
Author: User
How to add the same key values of the two functions to form a new array. $ T1 = Array ([0] => 1320 [1] => 990 [2] => 895 [3] => 790 [4] => 775 [5] => 745 [6] => 740 [7] => 680 [8] => 665 [9] => 535)
$ T2 = Array ([0] => Nanchang [1] => Ganzhou [2] => Ji 'an [3] => Jiujiang [4] => Nanchang [5] => jiujiang [6] => Pingxiang [7] => outside the province [8] => Nanchang [9] => Jiujiang)

$ T1 and $ t2 have the same length and their values correspond to each other,
For example
[0] = & gt; 1320
[0] => Nanchang
The number representing Nanchang is 1320. now I want to add the same values in $ t2.

For example, Nanchang
[0] = & gt; 1320
[0] => Nanchang
---------------------
[4] = & gt; 775
[4] => Nanchang
-----------------
[8] = & gt; 665
[8] => Nanchang
How to add the three numbers.

Form the following array:
$ T3 = Array ([2760] => Nanchang [990] => Ganzhou [895] => Ji 'an [2070] => Jiujiang [740] => Pingxiang [680] => outside the province )?
If you use the array_combine function, consider the case where the same key name is ignored.


Reply to discussion (solution)

$ T1 = array (0 => 1320, 1 => 990, 2 => 895, 3 => 790, 4 => 775, 5 => 745, 6 => 740, 7 => 680, 8 => 665, 9 => 535); $ t2 = array (0 => 'nanchang ', 1 => 'ganzhou ', 2 => 'Ji 'an ', 3 => 'jiujiang', 4 => 'nanchang ', 5 => 'jiujiang', 6 => 'pingxiang ', 7 => 'outside the name', 8 => 'nanchang ', 9 => 'jiujiang'); foreach ($ t2 as $ I => $ k) @ $ t3 [$ k] + = $ t1 [$ I]; $ t3 = array_flip ($ t3); print_r ($ t3 );
Array ([2760] => Nanchang [990] => Ganzhou [895] => Ji 'an [2070] => Jiujiang [740] => Pingxiang [680] => outside the province)

$ T1 = array (0 => 1320, 1 => 990, 2 => 895, 3 => 790, 4 => 775, 5 => 745, 6 => 740, 7 => 680, 8 => 665, 9 => 535); $ t2 = array (0 => 'nanchang ', 1 => 'ganzhou ', 2 => 'Ji 'an ', 3 => 'jiujiang', 4 => 'nanchang ', 5 => 'jiujiang', 6 => 'pingxiang ', 7 => 'outside the name', 8 => 'nanchang ', 9 => 'jiujiang'); foreach ($ t2 as $ I => $ k) @ $ t3 [$ k] + = $ t1 [$ I]; $ t3 = array_flip ($ t3); print_r ($ t3 );
Array ([2760] => Nanchang [990] => Ganzhou [895] => Ji 'an [2070] => Jiujiang [740] => Pingxiang [680] => outside the province)

What if the key name is the same? What should I do? For example,
Array ([990] => Nanchang [990] => Ganzhou [895] => Ji 'an [2070] => Jiujiang [740] => Pingxiang [680] => outside the province)

In this case, it will be overwritten. what should I do?

No way. your solution is inherently inadequate!

No way. your solution is inherently inadequate!
$ T1 = Array ([0] => 1320 [1] => 990 [2] => 895 [3] => 790 [4] => 775 [5] => 745 [6] => 740 [7] => 680 [8] => 665 [9] => 535)
$ T2 = Array ([0] => Nanchang [1] => Ganzhou [2] => Ji 'an [3] => Jiujiang [4] => Nanchang [5] => jiujiang [6] => Pingxiang [7] => outside the province [8] => Nanchang [9] => Jiujiang)

Then I want to calculate the numbers of different values in t2. Is there any way?


No way. your solution is inherently inadequate!
$ T1 = Array ([0] => 1320 [1] => 990 [2] => 895 [3] => 790 [4] => 775 [5] => 745 [6] => 740 [7] => 680 [8] => 665 [9] => 535)
$ T2 = Array ([0] => Nanchang [1] => Ganzhou [2] => Ji 'an [3] => Jiujiang [4] => Nanchang [5] => jiujiang [6] => Pingxiang [7] => outside the province [8] => Nanchang [9] => Jiujiang)

Then I want to calculate the numbers of different values in t2. Is there any way?

You can create a $ t11 = record value,
$ T22 = This is the recorded region.
This ensures that there will be no duplicate names, but I don't know if I can calculate the same sum.

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.