-1

I have two arrays in my PHP like following:

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

$brandListAll returns all of the items from my table, which can be lets say sized of 70, while brandListUserRegistered can be sized of 2 for instance. What I'm trying to achieve is to add the items to $brandListUserRegistered that are present in $brandListAll.

So if brandListUserRegistered is = 2 and $brandListAll is 70; I wanna add 68 items frmo brandListall that are missing in $brandListUserRegistered;

can someone help me out with this?

Edit:

The array $brandListUserRegistered looks like this:

[{
  "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"
  }]

The array $brandListAll looks like this:

[{
  "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"
  }

The array #1 contains information of userId which array #2 doesn't have...
But I still need them merged (excluding the elements that are already with the same ID in Array #1 like in Array #2)...

doydoy44
  • 5,720
  • 4
  • 29
  • 45
perkes456
  • 1,163
  • 4
  • 25
  • 49

2 Answers2

2

Have you try array_merge ?

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

$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 ); 
callmemath
  • 8,185
  • 4
  • 37
  • 50
  • Have you an object or array ? show the result of `print_r(brandListUserRegistered)` – callmemath Jun 07 '16 at 13:25
  • $brandListAll and $brandListUserRegistered are differently sized, and elements in $brandListUserRegistered contain some informations that are not present in $brandListAll... – perkes456 Jun 07 '16 at 13:38
  • If you have an array of object, you can't use array_merge. Tell me if you have an mutlidimensionnal array or an array of object – callmemath Jun 07 '16 at 13:40
  • Is it possible to merge two arrays like that, excluding the items which contain same brandId in both of the arrays?? – perkes456 Jun 07 '16 at 13:51
  • So it's different, it's an array of object check my edit. Thanks to http://stackoverflow.com/questions/11877586/how-to-merge-two-arrays-of-object-in-php – callmemath Jun 07 '16 at 14:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114042/discussion-between-perkes456-and-mathieu-lescaudron). – perkes456 Jun 07 '16 at 14:49
0
foreach($brandListAll as $bA) {
    $brandListUserRegistered['brandedAll'] = $bA;
}

You can do like this. if you want the record in the separate key of $brandListUserRegistered.

Edit

Try this.

foreach($brandListAll as $bA) {
    $bResult[] = $bA;
}
$brandListUserRegistered['brandedAll'] = $bResult;
Keyur
  • 1,113
  • 1
  • 23
  • 42
  • should this work even if the two arrays don't contain the same amount of the columns inside them.. For instance $brandListAll doesn't containts userId while $brandLisUserRegistered contains column named "userId" ... ? – perkes456 Jun 07 '16 at 13:40
  • If you need a better answer, show us the data of $brandListUserRegistered and $brandListAll – callmemath Jun 07 '16 at 13:42
  • @Keyur can u check my new question: http://stackoverflow.com/questions/37681800/returning-missing-results-from-many-to-many-table – perkes456 Jun 07 '16 at 14:17
  • Yes sure. @perkes456 – Keyur Jun 08 '16 at 05:06