Page 1 of 1

array advice pleeease :)

Posted: Mon Jan 28, 2008 6:54 am
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:

Re: array advice pleeease :)

Posted: Mon Jan 28, 2008 10:41 am
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?

Re: array advice pleeease :)

Posted: Mon Jan 28, 2008 10:47 am
by JAM
You need 2 arrays for this tho?

Re: array advice pleeease :)

Posted: Mon Jan 28, 2008 2:16 pm
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:

Re: array advice pleeease :)

Posted: Mon Jan 28, 2008 2:21 pm
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.