Ask an array question

Source: Internet
Author: User
I have an array of data above. can I change it to the following? The main idea is to merge the same places in the two arrays, store the different places, and re-open an array.

I have an array of data above. can I change it to the following?

The main idea is to merge the same places in the two arrays, store the different places, and re-open an array.

Reply content:

I have an array of data above. can I change it to the following?

The main idea is to merge the same places in the two arrays, store the different places, and re-open an array.

You need to use some specific fields repeatedly, instead of repeating them. you can try the following statement:

   $simiArray = [];    foreach($resourceArray as $rVal) {        $order_sn = $rVal['order_sn'];        $simiArray[$rVal['order_sn']]['order_sn'] = $rVal['order_sn'];        $simiArray[$order_sn]['order_add_time'] = $rVal['order_add_time'];        $simiArray[$rVal['order_sn']]['pay_amonut'] = $rVal['pay_amonut'];        unset($rVal['order_sn']);        unset($rVal['order_add_time']);        unset($rVal['pay_amonut']);        $simiArray[$order_sn]['order_detail'][] = $rVal;    }

Yes, but the standard library should be a function that does not have this function. you must write it yourself.

It is not convenient to write code now. let's give you a thought:

When two arrays are input, traverse the first array. if the values of the current keys of the two arrays are the same, push the values to a new array, and delete the key value from the two input arrays. If the values in the two arrays are different, you don't have to worry about it.

After processing, we can get an array that stores the same key-value pairs between two arrays, and two arrays that only store different contents (because the same one is deleted above ), finally, push the last two arrays into the first array.

PS: removing elements from an array may cause performance problems. you can store two arrays separately.

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.