Function merge arrays or objects by key. PHP code example open

Function merge arrays or objects by key. PHP code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

[/php]
function merge_objects( $users, $data, $key ) {
foreach ( $users as $k => $user ) {
foreach ( $data as $d ) {
if ( is_array( $d ) ) {
if ( $d[‘ID’] != $user->ID ) {
continue;
}
$users[ $k ]->$key = $d[ $key ];
} else {
if ( $d->ID != $user->ID ) {
continue;
}
$users[ $k ]->$key = $d->$key;
}
}
}

return $users;
}

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation