array advice pleeease :)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
soopa
Forum Newbie
Posts: 2
Joined: Mon Jan 28, 2008 6:44 am

array advice pleeease :)

Post by soopa »

simplified my post...

when looping thru my array i get:

117=>Yes
117=>true
118=>Yes
118=>true

what i want to achieve is:
if both lines '117 is yes' and '117 is true' are present, i want to enter a timestamp into the database where id=117
repeating the code for each number that crops up, eg: 118, 119, 120 etc

can i compare or look for parts of an array somehow?

if the opposite values get passed thru the array would read:
117=>No
118=>No

i guess if a val is yes or no then add the id to list A, if the val is true then add the id to list B. so now i have:

list A - list B
117 - 117
118 - 118
119 - 120
120

how do i compare these two lists to give me a final list of ids that appear in both A and B ??! :roll: :lol:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: array advice pleeease :)

Post by pickle »

You can't have the same key have 2 different values, so your initial array can't be

Code: Select all

$myArray = array(117=>'Yes',
                 117=>true,
                 118=>'Yes',
                 118=>true);
What does your array really look like?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: array advice pleeease :)

Post by JAM »

You need 2 arrays for this tho?
Last edited by JAM on Mon Jan 28, 2008 3:08 pm, edited 1 time in total.
soopa
Forum Newbie
Posts: 2
Joined: Mon Jan 28, 2008 6:44 am

Re: array advice pleeease :)

Post by soopa »

sorted it!

array_intersect

takes 2 or more arrays and returns an array containing elements that appear in all of the original arrays :lol:

thanks guys, i think just writing down my problems sometimes helps me figure things out :wink:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: array advice pleeease :)

Post by pickle »

array_intersect() does just what you want if you already have 2 arrays. From your original post though, it sounds like you're starting with just 1 array. Is that truly the case? If so, there might not be any reason to break it up into two separate arrays, just to run them through array_intersect() - might as well do the intersect logic on your first iteration.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply