【问题标题】:Compare two arrays in PHP and add missing items to smaller array比较 PHP 中的两个数组并将缺失的项目添加到较小的数组中
【发布时间】:2016-06-07 12:49:13
【问题描述】:

我的 PHP 中有两个数组,如下所示:

$brandListAll = $this->getBrandModel()->brandList();
$brandListUserRegistered = $this->getBrandUserModel()->getUserBrands($userId);
for($i=0; $i<count($brandListAll);$i++)
{
}

$brandListAll 返回我表中的所有项目,例如可以说大小为 70,而brandListUserRegistered 的大小可以为 2。我想要实现的是将项目添加到$brandListUserRegistered 中存在的$brandListAll

所以如果 brandListUserRegistered is = 2$brandListAll is 70; 我想添加 $brandListUserRegistered 中缺少的 68 项 frmo brandListall;

有人可以帮我解决这个问题吗?

编辑:

数组$brandListUserRegistered 如下所示:

[{
  "id":"64",
  "brandId":"64",
  "userId":"2869",
  "points":"0",
  "lifePoints":"0",
  "days":"1",
  "lifeDays":"1",
  "level":"0",
  "active":"1",
  "joinTime":"2016-06-06 12:42:08",
  "lastSignInTime":null,
  "lastSignInIp":null,
  "missions":null,
  "vipCode":null,
  "col1":null,
  "col2":null,
  "col3":null,
  "col4":null,
  "col5":null,
  "name":"brand1"
  }]

数组$brandListAll 如下所示:

[{
  "id":"1",
  "name":"brand2",
  "icon":"brand_552cde1109944.png",
  "owner":"1",
  "slogan":"Award-winning marketing building brands with consumers ! WOW",
  "banner":"brand_552ce25ba80e7.png",
  "homepage":"http:\/\/mdev.advocacy.asia",
  "active":"1",
  "cover":"brand_552ce25fd2492.png",
  "startDate":"2015-04-10",
  "appId":"1",
  "createTime":"2015-04-14 11:48:43",
  "endDate":"2016-06-30",
  "State":"Completed"
  }

数组#1 包含userId 的信息,而数组#2 没有...
但我仍然需要将它们合并(不包括在 Array #1 中已经具有相同 ID 的元素,如在 Array #2 中)...

【问题讨论】:

  • 尝试使用php array_diff()函数。

标签: php arrays zend-framework zend-framework2


【解决方案1】:

你试过array_merge吗?

将一个或多个数组的元素合并在一起,使值 一个附加到前一个的末尾。它返回 结果数组。

$brandListAll = $this->getBrandModel()->brandList();
$brandListUserRegistered = $this->getBrandUserModel()->getUserBrands($userId);

$result = array_merge($brandListUserRegistered,$brandListAll);
// remove duplicate objects
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
// sort by id
sort( $result ); 

【讨论】:

  • 你有对象或数组吗?显示print_r(brandListUserRegistered)的结果
  • $brandListAll 和 $brandListUserRegistered 的大小不同,$brandListUserRegistered 中的元素包含一些 $brandListAll 中不存在的信息...
  • 如果你有一个对象数组,你不能使用array_merge。告诉我你是否有一个多维数组或一个对象数组
  • 是否可以像这样合并两个数组,不包括两个数组中包含相同brandId的项目??
  • 所以它是不同的,它是一个对象数组检查我的编辑。感谢stackoverflow.com/questions/11877586/…
【解决方案2】:
foreach($brandListAll as $bA) {
    $brandListUserRegistered['brandedAll'] = $bA;
}

你可以这样做。如果您想要 $brandListUserRegistered 的单独键中的记录。

编辑

试试这个。

foreach($brandListAll as $bA) {
    $bResult[] = $bA;
}
$brandListUserRegistered['brandedAll'] = $bResult;

【讨论】:

  • 即使这两个数组不包含相同数量的列,这项工作是否应该有效。例如,$brandListAll 不包含 userId,而 $brandLisUserRegistered 包含名为“userId”的列。 . ?
  • 如果您需要更好的答案,请向我们展示 $brandListUserRegistered 和 $brandListAll 的数据
  • @Keyur 你能检查我的新问题吗:stackoverflow.com/questions/37681800/…
  • 是的,当然。 @perkes456
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2020-08-28
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多