Php array problem, please help

Source: Internet
Author: User
I asked a question in segmentfault for the first time. Now I have encountered a question. I hope you can answer it. Thank you very much !!! The problem is that there are two Arrays: $ arr1 and $ arr2 {code ...} {code ...} the sum of the key values of the two arrays is equal, for example, 1000, all... I asked a question in segmentfault for the first time. Now I have encountered a question. I hope you can answer it. Thank you very much !!!
The problem is that there are two Arrays: $ arr1 and $ arr2.

$arr1 = (    array('num'=>'500',id=>'a1'),    array('num'=>'300',id=>'a2'),    array('num'=>'200',id=>'a3'));
$arr2 = (    array('num'=>'400',id=>'b1'),    array('num'=>'200',id=>'b2'),    array('num'=>'200',id=>'b3'),    array('num'=>'100',id=>'b4'),    array('num'=>'100',id=>'b5'));

The sum of the key values of the two arrays is equal. For example, all values are 1000. All num values are multiples of 100. The id value is unique, but the length of the two arrays is not fixed; how to combine an array like this

$ Arr = (array (aid => 'a1', bid => 'b1', 'num' => '123 '), // b1 end array (aid => 'a1', bid => 'b2', 'num' => '123 '), // put the remaining 100 of the num value of a1 into the b2 a1 end array (aid => 'a2 ', bid => 'b2', 'num' => '123 '), // obtain the b2 end array (aid => 'a2 ', bid => 'b3', 'num' => '123') from 100 of b2 difference '), // put the remaining 200 of a2 to b3 a2 and end b3. array (aid => 'a3 ', bid => 'b4', 'num' => '123 '), // a3 get 100 to b4 b4 end array (aid => 'a2 ', bid => 'b5', 'num' => '123 '), // The upstream and downstream 100 of a3 end a3 with b5 );

Please ignore my little white situation and help me read this question. Thank you again!

Reply content:

I asked a question in segmentfault for the first time. Now I have encountered a question. I hope you can answer it. Thank you very much !!!
The problem is that there are two Arrays: $ arr1 and $ arr2.

$arr1 = (    array('num'=>'500',id=>'a1'),    array('num'=>'300',id=>'a2'),    array('num'=>'200',id=>'a3'));
$arr2 = (    array('num'=>'400',id=>'b1'),    array('num'=>'200',id=>'b2'),    array('num'=>'200',id=>'b3'),    array('num'=>'100',id=>'b4'),    array('num'=>'100',id=>'b5'));

The sum of the key values of the two arrays is equal. For example, all values are 1000. All num values are multiples of 100. The id value is unique, but the length of the two arrays is not fixed; how to combine an array like this

$ Arr = (array (aid => 'a1', bid => 'b1', 'num' => '123 '), // b1 end array (aid => 'a1', bid => 'b2', 'num' => '123 '), // put the remaining 100 of the num value of a1 into the b2 a1 end array (aid => 'a2 ', bid => 'b2', 'num' => '123 '), // obtain the b2 end array (aid => 'a2 ', bid => 'b3', 'num' => '123') from 100 of b2 difference '), // put the remaining 200 of a2 to b3 a2 and end b3. array (aid => 'a3 ', bid => 'b4', 'num' => '123 '), // a3 get 100 to b4 b4 end array (aid => 'a2 ', bid => 'b5', 'num' => '123 '), // The upstream and downstream 100 of a3 end a3 with b5 );

Please ignore my little white situation and help me read this question. Thank you again!

I think so about this question. Think of arr1 and arr2 as two groups of buckets,$arr1There are three buckets containing 500,300,200 of water,$arr2There are 5 empty buckets, and thenarr1Pouring Water inarr2.


  '123', 'id' => 'a1'), array ('num' => '123', 'id' => 'a2 '), array ('num' => '000000', 'id' => 'a3 '); $ arr2 = array ('num' => '20140901 ', 'id' => 'b1 '), array ('num' => '000000', 'id' => 'b2 '), array ('num' => '000000', 'id' => 'b3'), array ('num' => '2016 ', 'id' => 'b4 '), array ('num' => '000000', 'id' => 'b5'); st ($ arr1, $ arr2); // pouring water from $ a to $ B's bucket $ a =>$ bfunction st ($ a, $ B) {$ a_len = count ($ ); $ B _len = count ($ B); $ I = 0; // The current location of $ a $ j = 0; // The current location of $ B Set $ result = array (); while ($ I <$ a_len & $ j <$ B _len) {$ item_1 = $ a [$ I]; $ item_2 = $ B [$ j]; $ item = array (); $ item_1 ['num'] = intval ($ item_1 ['num']); $ item_2 ['num'] = intval ($ item_2 ['num']); if ($ item_2 ['num']> $ item_1 ['num']) {// the bucket of B is larger than that of a. After a is dumped, the bucket of B is taken from, therefore, $ I ++ // all the water in the bucket with the num value of a $ item = array ('aid '=> $ item_1 ['id'], 'bid' => $ item_2 ['id'], 'num' => $ item_1 ['num']); $ B [$ j] ['num']-= $ item_1 ['num']; // The water that can be poured into the bucket B $ I ++;} else if ($ item_2 ['num'] <$ item_1 ['num']) {// the bucket of B is small and will overflow, so the bucket of B is full. Take another bucket after it is fully filled. Therefore, the value of $ j ++ // num is the volume of B. $ item = array ('aid '=> $ item_1 ['id'], 'bid' => $ item_2 ['id'], 'num' => $ item_2 ['num']); $ a [$ I] ['num']-= $ item_2 ['num']; $ j ++ ;} else {$ item = array ('aid '=> $ item_1 ['id'], 'bid' => $ item_2 ['id'], 'num' => $ item_2 ['num']); $ I ++; $ j ++;} array_push ($ result, $ item);} echo"
";    print_r($result);}

From the example, array 1 can be understood as income, array 2 can be understood as expenditure, and the so-called combination result is similar to the balance change process, however, it is meaningless to specify which income is used for expenditure. Therefore, the combination of the source code is useless, but the requirement is still needed. If you think that the disclosure requirement violates the confidentiality agreement, do not ask.
The result of hard competition is not no good, but no value.

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.