Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

Compare two associative arrays problem.

Not very good at explaining these things so will try my best.

I have two associative arrays:

array(1)

  ["result"]=> array(2) { 
                                    ["status"]=> int(1) 
                                    ["names"]=> array(1209) {
 [0]=> array(2) {
 ["id"]=> int(58725218) 
 ["defindex"]=> int(4036) 



array(2)

  ["result"]=> array(2) { 
                                    ["status"]=> int(1) 
                                    ["names"]=> array(4000) {
 [0]=> array(3) {
 ["id"]=> int(58725218) 
["name"]=>string("robert") 
 ["defindex"]=> int(4036) 

I want to first check that if array1['result']['defindex'] matches a array2['result']['defindex'] .

Then I want to print out only the array2['result']["name"] for all that match but as array1 does not contain a ['name'] key I am now stunk on how I can do this.

As im new to PHP I checked the documentation and found array_intersect_assoc function but this only returns the the keys and values of those that match.

If I have made a balls of asking the question I do apologise but any help here would be amazing.

Thanks in advance to anyone who can help.

2 Answers

Hello,

By what you are asking, I created a simple script that does what you are asking for. I haven't tested the script but I know that it should work syntactically.

$count = (count(array1["names"]) <= count(array2["names"])) ? count(array1["names"]) : count(array2["names"]);

for($i=0; $i<$count; $i++) {
   if(array1["names"][$i]["defindex"] == array2["names"][$i]["defindex"]) {
      echo array2["names"][$i]["name"];
   }
}

LINE1: finds out if the size of array1 is less than or equal to array 2 then use the count for the smallest array. LINE2: uses a for loop to loop through both arrays LINE3: checks to see if the defindex for both arrays match LINE4: echos out the value in the name key of array2

I hope this what you are asking or close. I know you can probably do it some other way, but I go for simplicity since you don't know that much about PHP. Hope this what you are looking for; if not I can always modify it to your specs.

Cheers!

Robert Walker
Robert Walker
17,146 Points

No that does not work for me, I understand what that is doing too but I think I need something different than that.

Both arrays have a ['defindex']

array1 holds a list of employees in a particular department

array2 holds every employee in the company

So to get array1 I use a company api to return an array of ['names']=> array(1209} for the design team. The design team has 1209 employees.

Then using another api call I bring back array2 which is the full list of employees ["names"]=> array(4000) The company has 4000 employees.

I want to then match the [' defindex'] from both array1 and array2 removing all that do not match from array2.

It must leave other keys and values though, it can not just return an array with only ['defindex'] I want it to give me all of the ['names'] array key=>values from array2 if array1['defindex'] match.

Then store what is left from array2 into a variable and foreach loop over it printing out their names and other key=>values from that array.

As I say I am really poor at explaining this but hopefully you can understand what I mean.