Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2012, 03:12 PM   PM User | #1
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
array question

Hi

I've almost solved my array problem, but am just wondering if there is a better way to do this?

I have an array of ids stored in a $_SESSION var (from a previous page load), and when the page loads again, it gets a new array of ids from mysql. I want to compare the arrays (in the fastest way possible) to see if they are different, and if so, go do myfunc(). I'm not worried if the order has changed, just if there is a new id or one missing.

My code first does a simple sizeof comparison, and if there is a difference, no need to do any more testing, go straight to myfunc().

Bu if the sizes are the same, the id might still have changed, so my code then checks them like this:

PHP Code:
$new_array_size sizeof($new_array);

for(
$i 0$size $new_array_size$i $size; ++$i) {
            
   if (
$new_array[$i] != $_SESSION['last_array'][$i])
   {
      
myfunc ();                
      die();        
   }            

This works, except if the order of $_SESSION['last_array'] happens to get changed in another page.

I'm guessing the fix is to do a sort() of each array before the loop, but I'm wondering if there is a faster way overall to do it?

I tried figuring a way with isset after doing an array_flip, but array_flip didn't seem to swap keys and values in my arrays.

I would have used in_array, but I've read so many times that it's slow.

Thanks for your help.

Last edited by shaunthomson; 10-07-2012 at 03:17 PM..
shaunthomson is offline   Reply With Quote
Old 10-08-2012, 10:16 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,881
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
there are some neat little array functions like array_diff() or array_intersect() to compare arrays.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
shaunthomson (10-17-2012)
Old 10-17-2012, 03:25 AM   PM User | #3
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
Thanks Dormilich - I'll check them out.
shaunthomson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:20 PM.


Advertisement
Log in to turn off these ads.