Hi, Could someone point out what I'm missing on this search? Thanks for any help!

function valExists($needle, $haystack) 
{
		foreach($haystack as $k => $v) {
				if(is_array($v) && valExists($needle, $v) == true) { // check recursively
						return true; }
				if($needle == $v) {
						return true; }
		}
		return false;
}

    1.) What are you trying to do?

    2.) What are you expecting to happen?

    3.) What result do you see?

    4.) What error messages (if any, and check the server logs) are shown?

      Webnick: I find that it works correctly when I build my array correctly but it fails when I make a bad array.

      For example, if I search for the number 7 in this array, it will find it:

      $x[1] = 3;
      $x[2][1] = 4;
      $x[2][2] = 11;
      $x[3] = 13;
      $x[4][1] = 6;
      $x[4][2] = 7;
      $x[4][3] = 19;
      

      But if I search for the number 9 in this badly made array, it fails:

      $x[1] = 3;
      $x[2][1] = 4;
      $x[2][2] = 11;
      $x[3] = 13;
      $x[4] = 31;
      $x[4][1] = 6;
      $x[4][2] = 9;
      $x[4][3] = 18;
      

      So my question to you is, are you sure your array is sane?

        etully wrote:

        ...

        But if I search for the number 9 in this badly made array, it fails:

        $x[1] = 3;
        $x[2][1] = 4;
        $x[2][2] = 11;
        $x[3] = 13;
        $x[4] = 31;
        $x[4][1] = 6;
        $x[4][2] = 9;
        $x[4][3] = 18;
        

        That's because the last 3 elements are never added to the array in the first place, so of course the function will not find them.

        Warning: Cannot use a scalar value as an array in C:\wamp\www\test\test.php on line 7

        Warning: Cannot use a scalar value as an array in C:\wamp\www\test\test.php on line 8

        Warning: Cannot use a scalar value as an array in C:\wamp\www\test\test.php on line 9

          It should just return true if there's a match on earlier vals. the array looks like -

          Array
          (
              [0] => Array
                  (
                      [0] => #904 3D Modeler/Texture Artist
                      [1] => /recruiter/jobsview.php?RowId=904
                  )
          
          [1] => Array
              (
                  [0] => #904 3D Modeler/Texture Artist
                  [1] => /recruiter/jobsview.php?RowId=904
              )
          
          [2] => Array
              (
                  [0] => #904 3D Modeler/Texture Artist
                  [1] => /recruiter/jobsview.php?RowId=904
              )
          
          )
          $v ='jobview';
          $_SESSION[$v][] = array($title, $url);
          

            Are you looking for $v ('jobview') in that array?

            First of all, some of the elements in the array have "jobSview" (with an "s") as part of the string so jobview would never match jobsview.

            Second, your function is checking for an exact match so 'jobsview' would never match '/recruiter/jobsview.php?RowId=904' even if you put the "s" in $v when you are searching.

            Also, recursive functions are great when you need to drill down into some unknown number of levels (for example, into arrays of unknown complexity or into web sites that link to web sites that link to web sites) but your array seems simple enough that no recursive function would be necessary.

            It looks like instead of a recursive function, what you're really looking for is a function that does something like this:

            loop through all elements of the array {
            check to see if $searchstring exists anywhere inside $element[1] (use ereg for this)
            finish loop }

            I guess bpat was right - instead of fixing your code, I guess we need to understand what the goal of your application is.

              etully wrote:

              check to see if $searchstring exists anywhere inside $element[1] (use ereg for this)

              As long as the array structure is known to be constant, I agree with the new method you're suggesting, but you should never use any of the ereg() functions. These have long since been deprecated in favor of the more powerful, more efficient PCRE (preg()) functions.

              Having said that, using regular expressions period is still inefficient - using [man]strpos[/man] would be a better idea.

                you should never use any of the ereg*() functions. These have long since been deprecated

                Ah you young whipper snappers, with yer fancy new fangled contraptions. You probably want me to upgrade from my Apple ][+ too. Hey you kids, get off my lawn!

                  etully wrote:

                  You probably want me to upgrade from my Apple ][+ too.

                  Of course I want you to upgrade. 😃

                    Write a Reply...