senyo Posted October 24, 2009 Share Posted October 24, 2009 I am trying to create a loop in a big script. But I don't know what to do. I have an array, I take every value from it. I managed to make a if to compare the values, but I don't know what to write in it. If the values are equal then it should take $array[2] and compare it $gor[some]. How I make it to loop? if ($array[1]= $gor[some]) { //how to loop, how to make it compare $array[2] with $gor[some] } else { echo "x is smaller than y"; } Quote Link to comment Share on other sites More sharing options...
severndigital Posted October 24, 2009 Share Posted October 24, 2009 are you just trying to find out if $gor[some] is in $array? can you explain what the end goal of the loop will be? There are several ways easy to loop through that array, but each can have a different end result. Quote Link to comment Share on other sites More sharing options...
senyo Posted October 24, 2009 Author Share Posted October 24, 2009 This is what I want to do: I have an array with different values, I take them one by one $array[1], $array[2] etc to compare them with $gor[some] that is taken from a DB, if $array[1]==$gor[some] then it must go back and compare $array[2] and so on. If the values are not equal then the loop for this array[something] ends and continues the script. But the loop should continue comparing the next array array[something1] with $gor[some] Quote Link to comment Share on other sites More sharing options...
severndigital Posted October 24, 2009 Share Posted October 24, 2009 Ok .. so if understand right, you don't really have to use a loop for this although you could. you can use http://us2.php.net/manual/en/function.array-keys.php array_keys() but you need to put the optional param in. it would look something like this. $locationInArray = array_keys($array,$gor[some]); //this will return all the keys in the array that match $gor[some] I am still a bit unclear about what you want to have happen after the comparison is finished, but that could be my fault, it is quite early in the morning here Quote Link to comment Share on other sites More sharing options...
senyo Posted October 24, 2009 Author Share Posted October 24, 2009 I will write each condition manually, thanks Quote Link to comment Share on other sites More sharing options...
severndigital Posted October 24, 2009 Share Posted October 24, 2009 ok ... bear with me ..now i am confused. are you matching $gor[some] to mutiple arrays? Quote Link to comment Share on other sites More sharing options...
senyo Posted October 24, 2009 Author Share Posted October 24, 2009 Yes Quote Link to comment Share on other sites More sharing options...
severndigital Posted October 24, 2009 Share Posted October 24, 2009 ok .. that's a bit different that your first question. I have an array with different values... how are the arrays getting into this script? if they are separate arrays you will have to hard code each loop for each array. Quote Link to comment Share on other sites More sharing options...
deepson2 Posted October 24, 2009 Share Posted October 24, 2009 Hello senyo, What you are trying to do here i am not getting properly. Is that both the arrays are coming (fetched) from the database? are you running any query and getting both the arrays? or you want to do something like this $a = array('a', 'b'); $b = array('a', 'c'); $r1 = array_compare($b, $a); // false $r2 = array_compare($b, $a, false); // true Could you please be more specific so we will get the idea what you want to do exactly. Quote Link to comment Share on other sites More sharing options...
senyo Posted October 24, 2009 Author Share Posted October 24, 2009 both arrays are fetched and I want to compare an array with the content of another array Quote Link to comment Share on other sites More sharing options...
severndigital Posted October 24, 2009 Share Posted October 24, 2009 are you sure .... according to your first question you want to compare and array to a value which question are you trying to answer? is the value of $gor[some] in the array $array or does $array have the same values in it as the array $gor? Quote Link to comment Share on other sites More sharing options...
senyo Posted October 24, 2009 Author Share Posted October 24, 2009 I want to compare the value of an array, with a value from an array Quote Link to comment Share on other sites More sharing options...
deepson2 Posted October 24, 2009 Share Posted October 24, 2009 Try this- $matches = array_intersect($array1,$array2); if (count($matches) > 0) { // matches found so do you stuff. print_r($matches); // print the matches just to check. } Still i am not sure about what you are doing. could you please post your code snippet so we can get some idea what are you doing. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.