View Single Post
Old 07-19-2010, 03:20 PM   #1 (permalink)
danceRobot
The Wanderer
 
Join Date: Jul 2010
Posts: 6
Thanks: 1
danceRobot is on a distinguished road
Default Problems with in_array() in a callback function

Hey there!

1st of all, this is my first thread in this section... I hope its the right one since I dont know if it really is an beginners problem? but I assume it is one :) here we go:

I am trying to filter a array thats full of users, kicking out entrys that dont have a matching user_id so I only work with "buddys" out of a buddylist!

im foreach'ing the buddylist array and putting it into a new array because the current buddylist array has another array inside.. I only want the user id's!

then im calling array_filter(), passing it the user table and a callback function that has to check with in_array if the current user's ID is inside of the buddylist array and then removing/keeping the entry depending on the result...

soooo... the problem now is that if I check for any user Id with in_array() it works and finds the id. but if I use it inside of the callback function of array_filter() it tells me the second argument is a wrong datatype ????

heres the working "test" code without array_filter()
Code:
$tempArr = array();
foreach($this->buddyList["data"] as $buddyId){
 array_push($tempArr,$buddyId["relationid"]);
}
$this->buddyList = $tempArr;
print_r($this->buddyList);
if( in_array(1838,$this->buddyList) )
 echo "<br><br>FUCKING FOUND IT";
else
 echo "HELL NO....";
and here is the NOT WORKING code
Code:
$this->buddyList = $this->getUsersBuddyList($_GET['userId']);
$tempArr = array();
foreach($this->buddyList["data"] as $buddyId){
 array_push($tempArr,$buddyId["relationid"]);
}
$this->buddyList = $tempArr;
#GET LEADERBOARD STATS
$this->leaderboardStats = $this->getLeaderboardStats();
#FILTER ARRAY FRIENDS ONLY
function cmp_ptl_friends($x){
 if( in_array($x["user_id"],$engine->buddyList) ){
  return true;
 }else{
  return false;
 }
}
$this->leaderboardStats["data"] = array_filter($this->leaderboardStats["data"],'cmp_ptl_friends');
and the error message:

Code:
Warnung: in_array() [function.in-array]: Wrong datatype for second argument in /www/htdocs/xxxxxxxxx/XXXX.php  (Line 316)
I assume I am missing something... some php rule that doesnt let me use in_array() in a callback function??? I dont understand why it isnt working inside of the callback function, even tho I am using the same vars/arrays like in my test script???

this code is all inside the constructor of an object ($engine) I created. I was using $this->buddyList first but replaced it with $engine->buddyList because I got this error
Code:
Fatal error: Using $this when not in object context in /xxxxxxxx/XXXXXXX.php on line 316
I guess the whole problem has something to do with the buddylist and the way I have to call it inside the callback function of array_filter()

I appreciate any help on this :)
danceRobot is offline  
Reply With Quote