Home > Net >  PHP - Combine Two Arrays based on a key property value of the first array - improve code
PHP - Combine Two Arrays based on a key property value of the first array - improve code

Time:04-21

I have two associative arrays below

The first one has information about the stock status of that day, this is store in a different database.

The second array has the information of the product like name, description and so on.

I wish to merge or combine the two arrays based on a key property value (product_id) of the first array.

So far I'm using two foreach loops to combine the two. How can I improve my code ?

Example (the common key is product_id ) :

$array_one = [
    [
        'product_id' => 1,
        'product_stock' => '1.2',
        'stock_date' => '2022-02-15'
    ],
    [
        'product_id' => 2,
        'product_stock' => '5',
        'stock_date' => '2022-02-15'
    ],
];
$array_two = [
    [
        'product_id' => 1,
        'product_slug' => 'product_one',
        'product_description' => 'this is the product one'
    ],
    [
        'product_id' => 2,
        'product_slug' => 'product_two',
        'product_description' => 'this is the product two'
    ],
    [
        'product_id' => 3,
        'product_slug' => 'product_three',
        'product_description' => 'this is the product three'
    ],
];

so far i got :

$new_array = [];

foreach ($array_one as $key => $value) {
    $new_array[ $value['product_id'] ] = $value ;
}
foreach ($array_two as $key => $value) {
    if ( isset( $new_array[ $value['product_id'] ] ) ) {
        $new_array[ $value['product_id'] ]['product_slug'] = $value['product_slug'];
        $new_array[ $value['product_id'] ]['product_description'] = $value['product_description'];
    }
}

result as expected :

array(2) {
  [1]=>
  array(5) {
    ["product_id"]=>
    int(1)
    ["product_stock"]=>
    string(3) "1.2"
    ["stock_date"]=>
    string(10) "2022-02-15"
    ["product_slug"]=>
    string(11) "product_one"
    ["product_description"]=>
    string(23) "this is the product one"
  }
  [2]=>
  array(5) {
    ["product_id"]=>
    int(2)
    ["product_stock"]=>
    string(1) "5"
    ["stock_date"]=>
    string(10) "2022-02-15"
    ["product_slug"]=>
    string(11) "product_two"
    ["product_description"]=>
    string(23) "this is the product two"
  }
}

CodePudding user response:

Modify your second array first, so that it uses the product ID as key - that makes the look-up of the item corresponding to the product ID from the first array much easier.

$array_two = array_combine(array_column($array_two, 'product_id'), $array_two);
$new_array = [];
foreach($array_one as $product) {
    $product['product_slug'] = $array_two[$product['product_id']]['product_slug'];
    $product['product_description'] =
         $array_two[$product['product_id']]['product_description'];
    $new_array[$product['product_id']] = $product;
}

CodePudding user response:

One option you can use the array_map function:

$n_arr = array_map(function ($row) use ($array_two){
    foreach ($array_two as $item){
        if($item['product_id'] == $row['product_id']){
            return $item   $row;
        }
    }
    return false;
},$array_one);
  • Related