Php 如何将两个数组映射为一个?

Php 如何将两个数组映射为一个?,php,arrays,Php,Arrays,在我像这样合并两个数组之后,array\u merge($array1,$array2)它变成这样 array(10) { [0]=> object(stdClass) (2) { ["id"]=> string(1) "1" ["text"]=> string(5) "one" } [1]=> object(stdClass) (2) { ["id"]=> string(1) "2" ["

在我像这样合并两个数组之后,
array\u merge($array1,$array2)它变成这样

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)
  }
}
这意味着两个数组实际上是合并的。但我想要的是,因为两个数组都有一个名为id的公共属性和相同的值

它应该变成:

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)
    }
  }
注意,arra1有id,text,而array2有id,unit

我确实在这里引用了第一个建议使用数组映射的答案,但是对于我来说,我得到的错误是参数1不是数组

编辑:

尝试了以下方法(无效):


合并对象明显比数组更乏味。在我自己的项目中,我很想将对象数组转换为一个或多个数组,但我不支持这种解决方案

与可以使用
array\u merge()
或union操作符的数组不同,对象需要手动推入

我通过使用
id
值作为循环中的第一级键对数据进行临时分组,然后根据这些键进行选择性排序,然后重新索引输出以删除临时键

代码:():

或:


更好的做法是不要合并twp输入数组以形成
$poully\u merged
数组。您可以使用迭代第二个对象数组并将数据添加到第一个数组中,这将是一个更直接的解决方案。

因为您有对象数组,您需要“手动”遍历它们,或者将它们转换为数组数组first@MarkBaker,你能告诉我怎么做吗,我不知道如何将它们转换成数组?
array\u walk($myArray,function(&$value){$value=(array)$value;})
array\u walk()
是“通过引用传递”;返回值是布尔成功/失败,因此不要将其分配给数组,否则将覆盖array@112233我原来的答案是垃圾,我已经完全覆盖了它。@Dharman当这个副本关闭时,我将删除这个次优的答案。
$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);
$output = [];
foreach ($poorly_merged as $object) {
    if (!isset($output[$object->id])) {
        $output[$object->id] = $object;
    } else {
        foreach ($object as $property => $value) {
            $output[$object->id]->{$property} = $value;
        }
    }
}
ksort($output); // optionally order by ids
var_export(array_values($output));
$output = [];
foreach ($poorly_merged as $object) {
    if (!isset($output[$object->id])) {
        $output[$object->id] = (object)[];
    }
    foreach ($object as $property => $value) {
        $output[$object->id]->{$property} = $value;
    }
}
ksort($output); // optionally order by ids
var_export(array_values($output));