How to match two arrays into one?

After I combined the two arrays of type array_merge($array1, $array2);, it will become like this,

array(10) {
  [0]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "1"
    ["text"]=>
    string(5) "one"
  }
  [1]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "2"
    ["text"]=>
    string(8) "two"
  }
  [2]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "3"
    ["text"]=>
    string(4) "three"
  }
  [3]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "4"
    ["text"]=>
    string(8) "four"
  }
  [4]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "5"
    ["text"]=>
    string(3) "five"
  }
  [5]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "1"
    ["unit"]=>
    string(1) "0"
  }
  [6]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "2"
    ["unit"]=>
    int(0)
  }
  [7]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "3"
    ["unit"]=>
    int(0)
  }
  [8]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "4"
    ["unit"]=>
    string(1) "0"
  }
  [9]=>
  object(stdClass) (2) {
    ["id"]=>
    string(1) "5"
    ["unit"]=>
    int(1)
  }
}

This means that both arrays are literally merged. But I wanted, since both arrays have a common property called id and the same value for it,

It should look like this:

array(2) {
  [0]=>
    object(stdClass) (2) {
      ["id"]=>
      string(1) "1"
      ["text"]=>
      string(5) "one"
      ["unit"]=>
      int(0)
    }
    [1]=>
    object(stdClass) (2) {
      ["id"]=>
      string(1) "2"
      ["text"]=>
      string(8) "two"
      ["unit"]=>
      int(2)
    }
  }

Note that arra1 has id, text, while array2 has id, unit

I actually referenced here, as I tried the first answer, which suggests using array_map, but for me I get an error because argument 1 is not an array.

Combine two arrays into a single array with a common value

EDIT:

tried (doesn't work):

$array1 = array_walk($array1, function(&$value) { $value = (array) $value; })
$array2 = array_walk($array2, function(&$value) { $value = (array) $value; })
function modifyArray($a, $b)
{
    if (!empty($a) && !empty($b)) {
        return array_merge($a, $b);
    } else if (!empty($a) && empty($b)) {
        return $a;
    }  else if (empty($a) && !empty($b)) {
        return $b;
    }
}

$new = array_map("modifyArray", $array1, $array2);
var_dump($new);
+4
source share
1 answer

. $poorly_merged . , , . (ints/strings). , , , .

:

<?php
$poorly_merged=array(
    array("id"=>"5","unit"=>1),
    array("id"=>"3","text"=>"three"),
    array("id"=>"1","text"=>"one"),
    array("id"=>"2","text"=>"two"),
    array("id"=>"4","text"=>"four"),
    array("id"=>"5","text"=>"five"),
    array("id"=>"3","unit"=>0),
    array("id"=>"1","unit"=>"0"),
    array("id"=>"2","unit"=>0),
    array("id"=>"4","unit"=>"0")
);

$ids_array=array_unique(array_column($poorly_merged,"id"));  // create array of unique ids
sort($ids_array);   // only necessary if ids are not in order already
$x=0;
foreach($ids_array as $id){
    $well_merged[$x]=array();  // must be declared so array_merge works later
    $q=array("id"=>$id);  // declare the identifying key-value pair (needle) to match
    // create an array only of rows where $q key-value pairs exist
    $qualifying_array=array_filter(
        $poorly_merged,  // haystack
        function($val)use($q){  // using needle
            if(sizeof(array_intersect_assoc($val,$q))==sizeof($q)){  // if #found = #needles
                return $val;  // it a keeper
            }
        },
        ARRAY_FILTER_USE_BOTH  // send key-value pairs through the function as $val
    );
    foreach($qualifying_array as $to_be_flattened){
        $well_merged[$x]=array_merge($well_merged[$x],$to_be_flattened);  // merge matches, overwriting duplicate id
    }
    ++$x;
}

echo "<pre>";
var_export($well_merged);
echo "</pre>";

:

array (
  0 => 
  array (
    'id' => '1',
    'text' => 'one',
    'unit' => '0',
  ),
  1 => 
  array (
    'id' => '2',
    'text' => 'two',
    'unit' => 0,
  ),
  2 => 
  array (
    'id' => '3',
    'text' => 'three',
    'unit' => 0,
  ),
  3 => 
  array (
    'id' => '4',
    'text' => 'four',
    'unit' => '0',
  ),
  4 => 
  array (
    'id' => '5',
    'unit' => 1,
    'text' => 'five',
  ),
)
+1

Source: https://habr.com/ru/post/1670999/


All Articles