Jump to content

Creating a loop


senyo

Recommended Posts

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";
} 

Link to comment
Share on other sites

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]

 

 

Link to comment
Share on other sites

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 :)

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.