-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)...

1
  • Try using php array_diff() function. Commented Jun 7, 2016 at 12:57

2 Answers 2

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 ); 
Sign up to request clarification or add additional context in comments.

6 Comments

Have you an object or array ? show the result of print_r(brandListUserRegistered)
$brandListAll and $brandListUserRegistered are differently sized, and elements in $brandListUserRegistered contain some informations that are not present in $brandListAll...
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
Is it possible to merge two arrays like that, excluding the items which contain same brandId in both of the arrays??
So it's different, it's an array of object check my edit. Thanks to stackoverflow.com/questions/11877586/…
|
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;

4 Comments

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" ... ?
If you need a better answer, show us the data of $brandListUserRegistered and $brandListAll
@Keyur can u check my new question: stackoverflow.com/questions/37681800/…
Yes sure. @perkes456

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.