TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-19-2010, 03:20 PM   #1 (permalink)
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
Old 07-19-2010, 07:19 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Normally I would tell you to global $engine and leave it at that, but I think we're only getting a chunk of code from the greater whole here; as suggested by $this I would presume that this is taken from a larger class that you are writing?

php Code:
public function cmp_ptl_friends( $x )
{
    if ( in_array( $x['user_id'], $this->buddyList ) )
....

$this->leaderboardStats['data'] = array_filter( $this->leaderboardStats['data'], array( $this, 'cmp_ptl_friends' ) );

The gist of the idea here is that function cmp_ptl_friends() should be a method of your class, and you can call it (other methods from the same class) from array_filter() using the syntax shown above.

Outside of that, if these were code snippets from a procedural script, your problem would have been the fact that cmp_ptl_friends() had no idea who or what $engine was. Since $engine was not created inside the function, nor was it passed as an argument, it doesn't yet exist in the scope of the function. You would have had to either pass it in as an argument or use the global keyword to bring it in from the global scope.
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
danceRobot (07-19-2010)
Old 07-19-2010, 07:44 PM   #3 (permalink)
The Wanderer
 
Join Date: Jul 2010
Posts: 6
Thanks: 1
danceRobot is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
php Code:
public function cmp_ptl_friends( $x )
{
    if ( in_array( $x['user_id'], $this->buddyList ) )
....

$this->leaderboardStats['data'] = array_filter( $this->leaderboardStats['data'], array( $this, 'cmp_ptl_friends' ) );
aaaahhh.... ok.... this enlightens me! :)
I didnt know that I could use "array($this,'functionName')" as the callback paramenter / I didnt know at all how to call a function of a class as the callback method...!!! I should have asked this in the first place I guess haha...

I will try this out soon, but it already seems you really know what you are talking about and it WILL WORK! :D
danceRobot is offline  
Reply With Quote
Old 07-19-2010, 09:28 PM   #4 (permalink)
The Wanderer
 
Join Date: Jul 2010
Posts: 6
Thanks: 1
danceRobot is on a distinguished road
Default

Sorry for the double-posting.... But I have to say IT WORKS !!!! :)
Big thx for the help with my first problem thread on this forum ;)
danceRobot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM


All times are GMT. The time now is 06:18 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design