Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace some values in multidimentional array with another array

I try to replace some value in a multidimensional array with another array with the same key but it turns out to replace all values

Here is my example array

[
    {
        "book_id": 45,
        "language_code": "RUWT-EN",
        "book_name": Study,
        "country": "Singapore",
        "created_by": 12,
        "created_date": "2019-04-09 09:19:24",
        "update_by": 12,
        "update_date": "2019-06-25 03:57:52",
        "status": "1",
    },
    {
        "book_id": 46,
        "language_code": "RUWT-EN",
        "book_name": Sleep,
        "country": "Indonesia",
        "created_by": 12,
        "created_date": "2019-04-09 09:19:24",
        "update_by": 12,
        "update_date": "2019-06-25 03:57:52",
        "status": "1",
    },
    {
        "book_id": 47,
        "language_code": "RUWT-EN",
        "book_name": Teaching,
        "country": "China",
        "created_by": 12,
        "created_date": "2019-04-09 09:19:24",
        "update_by": 12,
        "update_date": "2019-06-25 03:57:52",
        "status": "1",
    },
]

and this is my 2nd array

[
    {
        "book_id": 45,
        "language_code": "RUWT-CH",
        "book_name": Study in CH,
        "country": "Korea",
        "status": "2",
    },
    {
        "book_id": 46,
        "language_code": "RUWT-CH",
        "book_name": Sleep in CH,
        "country": "US",
        "status": "2",
    },
    {
        "book_id": 47,
        "language_code": "RUWT-CH",
        "book_name": Teaching in CH,
        "country": "England",
        "status": "2",
    },
]

I've tried using laravel map collection and foreach one by one values then replace the value that have the same key, but it was to long . i want easiest way

$result = $collect_real->map(function($item) use($lang){
              return $item['book_name'] = $lang->where('book_id', $item['book_id'])->values();
            });

i want result like this

[
    {
        "book_id": 45,
        "language_code": "RUWT-CH",
        "book_name": Study in CH,
        "country": "Korea",
        "created_by": 12,
        "created_date": "2019-04-09 09:19:24",
        "update_by": 12,
        "update_date": "2019-06-25 03:57:52",
        "status": "2",
    },
    {
        "book_id": 46,
        "language_code": "RUWT-CH",
        "book_name": Sleep in CH,
        "country": "US",
        "created_by": 12,
        "created_date": "2019-04-09 09:19:24",
        "update_by": 12,
        "update_date": "2019-06-25 03:57:52",
        "status": "2",
    },
    {
        "book_id": 47,
        "language_code": "RUWT-CH",
        "book_name": Teaching in CH,
        "country": "England",
        "created_by": 12,
        "created_date": "2019-04-09 09:19:24",
        "update_by": 12,
        "update_date": "2019-06-25 03:57:52",
        "status": "2",
    },
]
like image 341
Lalisa Hwang Avatar asked Jul 26 '26 00:07

Lalisa Hwang


1 Answers

The function array_replace_recursive solves your problem in one line of code:

From the docs:

array_replace_recursive() replaces the values of array1 with the same values from all the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.

array_replace_recursive() is recursive: it will recurse into arrays and apply the same process to the inner value.

Example:

$updatedBooks = array_replace_recursive($wrongBooksArray, $correctBooksArray);
like image 161
Caconde Avatar answered Jul 28 '26 15:07

Caconde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!