Jump to content

array_search in loop without succes


filoaman

Recommended Posts

Hi

I'm trying to use array_search in a loop without success. i will appreciate any feed back.

here is mu code:

$itemsToSearch=array("item1","item2","item3")
// An array with the tems I like to search

$ArrayToSearchIn=array("item1","item2","item3","item5","item5","item6")
// Here is that array I try to execute the search

$i=0;
while ($i<3){
$searchTerm=$itemsToSearch[$i];
$key=array_search($searchTerm, $ArrayToSearchIn);
print $key."<br>";
$i++;
}

Now what i get as result is something like this:

<br> (an empty line with line brake)

<br> (an empty line with line brake)

2 (the key number ONLY for the third item "item3" or "$itemsToSearch[2]")

 

I was expecting something like this:

(the key number for the first item "item1" or "$itemsToSearch[0]")

(the key number for the second item "item2" or "$itemsToSearch[1]")

(the key number for the first third "item3" or "$itemsToSearch[2]")

 

What is wrong with my code?

Any ideas?

Thanks in advance

Link to comment
Share on other sites

@vinny42

 

Assume that the syntax errors are i forgot to add the ";" symbol at the end of my original array declaration

$itemsToSearch=array("item1","item2","item3");
// An array with the tems I like to search

$ArrayToSearchIn=array("item1","item2","item3","item5","item5","item6");
// Here is that array I try to execute the search

$i=0;
while ($i<3){
$searchTerm=$itemsToSearch[$i];
$key=array_search($searchTerm, $ArrayToSearchIn);
print $key."<br>";
$i++;
}

Are there any other syntax errors?

The original code is almost identical. I only change the names of the variables in order to make things more clear for the users to help.

 

You mention that, apart from the syntax errors, you get the result i want to get using this code. 

Is that true?

Link to comment
Share on other sites

Have you considered array_intersect()?

$itemsToSearch=array("item1","item2","item3");
$ArrayToSearchIn=array("item1","item5","item2","item3","item5","item6");

$res = array_intersect($ArrayToSearchIn, $itemsToSearch);

echo '<pre>',print_r($res, true),'</pre>';

/****************************
OUTPUT

Array
(
    [0] => item1
    [2] => item2
    [3] => item3
)
*******************************/
Link to comment
Share on other sites

OK - I just realize what happen!

As vinny42 mention the code works. The problem was that i originally create the "$itemsToSearch" array from data posted from a form. So at the end of every "item" there was a brake line (\r).

Although I echo the "items" on screen, it was impossible to see the line brake!

​So although the array_search works perfect, can't find the "item\r" on "$ArrayToSearchIn" array.

Since the last "item" was entered in the "$itemsToSearch" array from the post form without (\r) brake, that's why i was able to get the $key only for the last "item"!

 

I clear the entries using this little line of code:

$searchTerm=str_replace("\r", '', $searchTerm);

and now everythig is fine!

 

Thank you for your help and you feedback!

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.