Home > Backend Development > PHP Tutorial > Asking for advice on an array question

Asking for advice on an array question

WBOY
Release: 2016-09-24 09:15:10
Original
1180 people have browsed it

Asking for advice on an array questionAsking for advice on an array question

I now have an array data above and want to change it to the following. Can I change it?

Asking for advice on an array question

The main thing is to merge the same places in the two arrays together, and then open a new array to save the different places

Reply content:

Asking for advice on an array questionAsking for advice on an array question

I now have the above array data and want to change it to the following one. Can I change it?

Asking for advice on an array question

The main thing is to merge the same places in the two arrays together, and then open a new array to save the different places

You probably want some specific fields to be taken out repeatedly for use. Instead of repeating them, you can try the following statement

<code>   $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;
    }
</code>
Copy after login

It can be implemented, but there should be no function with this function in the standard library, so you have to write it yourself.

It’s not convenient to write code now, so let me give you an idea first:

When two arrays are input, traverse the first array. If the current key values ​​of the two arrays are the same, push the value to a new array and delete the key value from the two input arrays. Don't worry if the values ​​in the two arrays are different.

After this processing, we can get an array that stores the same key-value pairs between the two arrays, and two arrays that only store different contents (because the same ones were deleted above), and finally we need to Just push the two arrays into the first array.

PS: Deleting elements from an array may cause performance problems. You can store them in two separate arrays

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template