Taking values from one multidimensional array and putting it in the other multidimensional array based on matching value from first key

Viewed 36

I have two arrays, $full_list and $only_titles with matching value based on first key.

$full_list:

Array
(
    [0] => Array
        (
            [0] => samsung-a40 
            [1] => Samsung A40 Phone
            [2] => 
            [3] => 21
            [4] => 334234
            [5] => 0          
        )

    [1] => Array
        (
            [0] => samsung-a72
            [1] => Galaxy A72 Phone
            [2] => 
            [3] => 230
            [4] => 239049
            [5] => 0    
        )
    [2] => Array
        (
            [0] => lg-k61
            [1] => LG K61 Phone 
            [2] => 
            [3] => 22
            [4] => 249582
            [5] => 0    
        )


  etc...

$only_titles:

Array
(
    [0] => Array
        (
            [0] => samsung-a40 
            [1] => Black Phone Case For Samsung A40               
        )

    [1] => Array
        (
            [0] => lg-k61
            [1] => Red Case LG K61 Phone            
        )

    [2] => Array
        (
            [0] => samsung-a72
            [1] => Folding Phone Case for Galaxy A72 Phone          
        )
   
  etc...

I want to take value from second key from $only_title array (its basicaly long product title) and replace it in the $full_list array:

$final_result:

Array
(
    [0] => Array
        (
            [0] => samsung-a40
            [1] => Black Phone Case For Samsung A40   // title from $only_titles
            [2] => 
            [3] => 21
            [4] => 334234
            [5] => 0          
        )

    [1] => Array
        (
            [0] => samsung-a72
            [1] => Folding Phone Case for Galaxy A72 Phone   // title from $only_titles     
            [2] => 
            [3] => 230
            [4] => 239049
            [5] => 0    
        )
    [2] => Array
        (
            [0] => lg-k61
            [1] => Red Case LG K61 Phone  // title from $only_titles
            [2] => 
            [3] => 22
            [4] => 249582
            [5] => 0    
        )
  etc...

I have tried array_replace_recursive(), but the problem with this is that it expects that the order of subarrays are the same. But as you can see in both arrays the second subarrays dont match. With array_replace_recursive() i get the second subarray wrong:

[1] => Array
    (
        [0] => samsung-a72
        [1] => Red Case LG K61 Phone  // this is wrong
        [2] => 
        [3] => 230
        [4] => 239049
        [5] => 0    
    )

How can I check if values from first key match?

2 Answers

This script will help you to solve your problem

<?php

$full_array = [
    [
        'samsung-a40',
        'Samsung A40 Phone',
        null,
        21,
        334234,
        0,
    ],
    [
        'samsung-a72',
        'Galaxy A72 Phone',
        null,
        230,
        239049,
        0,
    ],
];

$only_titles = [
    [
        'samsung-a40',
        'Black Phone Case For Samsung A40',
    ],
    [
        'samsung-a72',
        'Red Case LG K61 Phone  ',
    ],
];

$formattedOnlyTitles = [];
foreach ($only_titles as $value) {
    $formattedOnlyTitles[$value[0]] = $value[1];
}

$fullArrayWithNewTitle = [];
foreach ($full_array as $value) {
    if (isset($formattedOnlyTitles[$value[0]])) {
        $value[1] = $formattedOnlyTitles[$value[0]];
    }

    $fullArrayWithNewTitle[] = $value;
}

print_r($fullArrayWithNewTitle);

output

Array
(
    [0] => Array
        (
            [0] => samsung-a40
            [1] => Samsung A40 Phone
            [2] => 
            [3] => 21
            [4] => 334234
            [5] => 0
        )

    [1] => Array
        (
            [0] => samsung-a72
            [1] => Galaxy A72 Phone
            [2] => 
            [3] => 230
            [4] => 239049
            [5] => 0
        )
)

First I change the format of the title_only var to make is easy to work with it in the next foreach

$formattedOnlyTitles = [];
foreach ($only_titles as $value) {
    $formattedOnlyTitles[$value[0]] = $value[1];
}

In the next foreach I just check the title with my new formatted title_only array

if (isset($formattedOnlyTitles[$value[0]])) {
    $value[1] = $formattedOnlyTitles[$value[0]];
}

And then change the title if I need

You can utilize array_map and array_search for your task.

$items = array_map(function($item) use ($only_titles) {
  $index = array_search($item[0], array_column($only_titles, 0));
  if($index === false) return;

  $item[1] = $only_titles[$index][1]; 
  return $item;
}, $full_list);

array_map() takes $full_list and iterate each item as $item,
then for each item, we look for the matching title with array_search()
It will return an index from $only_titles.
We can use $index to reference and assign the long title to item.

The result are in $items

Related