Hi Folks,

I'm currently creating a website that is offerred in English, Italian and Spanish and I am currently creating a user search function. The code so far is:

for ($i=0;$i<count($search_words);$i++) {
$var=$search_words[$i];
$lang_arr=array(0 => array("english", "inglés", "ingles", "inglesi"),
					 1 => array("spanish", "espanol", "español", "spagnolo"),
					 2 => array("italian", "italiano")
					 );

if (in_array($var,$lang_arr)) { $var=array_search($var,$lang_arr); }

$var is definitely being assigned, and is converted to lower case further up in the code to ensure there are no case-sensitivity issues. I can't really see why this isn't working, but I did wonder whether in_array() might not support multi-dimension arrays, when searching the $lang_arr ?

I have to admit, I don't use many of the wealth of array functions that are on offer as much as I should and looking at them, they make life much easier than the alternatives I may have used from time to time. Any help would be greatly appreciated.

Many Thanks,

Tom

    in_array will not search within multi-dimensional arrays, for that you need something else. Go read the manual for lots of ways of doing it.

      Hello.

      Maybe this can give you a hint...this is for 2-D:

      for ( $x=0; $x < count($array); $x++ )
      {
      	for ( $y=0; $y < count($array[$x]); $y++ )
      	{
      		print "\$array[$x][$y]: ".$array[$x][$y]."<br>\n";
      	}
      }

      (note: the <br> is not <br />)

      I found this example in 'Sam's PHP4' book ((c) 2000) and have been using a version of it since.

      Works well for 'preg_match_all()'.

      HTH.

      EDIT: Just another thought: Stick an 'if' statement e.g. within the 'for ($y=...)' to capture e.g. $array[0][$y] and you can set it to some var for later use in 'in_array()'.

      Just noticed you have it resolved. Maybe someone can use this...😕

        Hi Sherry,

        Thanks for your reply. That is what I normally do, a loop inside a loop. I just occassionally find as a PHP coder that I don't know my toolbox as well as I should with some of the functions, and didn't know whether I was missing a trick here.

        Cheers,

        Tom

          Write a Reply...