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.