3

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",
    },
]
6
  • I do not understand what's your problem. Does your code work or not? Commented Jul 4, 2019 at 9:27
  • You have all records with same book id. Commented Jul 4, 2019 at 9:29
  • @GiacomoMasseroniChiaro it doesnt work, i want to replace the some value in the 1st array with the 2nd array Commented Jul 4, 2019 at 9:41
  • @DrakulaPredatorم ive edit it thank you Commented Jul 4, 2019 at 9:44
  • What is your actual behaviour. What's going wrong? Commented Jul 4, 2019 at 9:56

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This approach merges the row data based on the first level indexes. It does not relate rows based on the shared book_id values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.