比较PHP中的两个数组并将缺少的项添加到较小的数组中

时间:2016-06-07 12:49:13

标签: php arrays zend-framework zend-framework2

我的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没有......的信息 但是我仍然需要将它们合并(不包括在数组#1中已经具有相同ID的元素,就像在数组#2中一样)...

2 个答案:

答案 0 :(得分:1)

您试试array_merge吗?

  

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

'Right?' -replace '\?', '!' # -> 'Right!'

答案 1 :(得分:0)

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

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

修改

试试这个。

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