Add two arrays to write one array to another.

Source: Internet
Author: User
Tags what array
Add two arrays and write an array to the known arrays a and B of another array:

// Array a: array (0 => array ('cust _ no' => '310f6 1VA5A ', 'lotno' => '2x15 ', 'part _ count' => '0',), 1 => array ('cust _ no' => '310f6 1VA5A ', 'lotno' => '2z25 ', 'part _ count' => '5',), // array B: array (0 => array ('cust _ no' => '310f6 1VA5A ', 'lotno' => '2z15', 'part _ count' => '000000',), 1 => array ('cust _ no' => '310f6 1VA5A ', 'lotno' => '123', 'part _ count' => '28 ',),)


Add the result of array B to array a, and accumulate the part_count corresponding to cust_no and lotno. The result is as follows:
array (  0 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2X15',    'part_count' => '0',  ),  1 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2Z15',    'part_count' => '105',  ),  2 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '3115',    'part_count' => '28',  ),)

How can this be done? What array functions are required? Thank you!


Reply to discussion (solution)

$t=array();foreach(array_merge($a,$b) as $v){     if(!isset($t[$v['cust_no'].'_'.$v['lotno']])){     $t[$v['cust_no'].'_'.$v['lotno']]=$v; }else{      $t[$v['cust_no'].'_'.$v['lotno']]['part_count']+=$v['part_count']; } } print_r(array_values($t));


$ V ['cust _ no']. '_'. $ v ['lotno?

Combine the values of cust_no and lotno into a key to judge the repeated

Combine the values of cust_no and lotno into a key to judge the repeated
Oh, you can use it like this. 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.