bpat1434's example has a bug though, and it is up to you to fix it.

Hint: array_search() returns false if the search value is not found. What does (false == 0) evaluate to?

Once you figure out the fix, you can eliminate one of the (count($array)-1) expressions.
Also, if you are using PHP5, you may find using [man]array_slice/man with preserve_keys better than using array_splice().

    owh..i am using php 4.3.3

    guguu~~..i am beginning to feel like drowning in the quicksand..these FYP of mine is becoming more and more difficult to solve each days..

    still trying to do some editting though~~..

    thank you guys for helping me~~..anybody else wanna give some ideas..you are soooo welcomed...

    pity me - helplesss Final Year student-

      Hint: array_search() returns false if the search value is not found. What does (false == 0) evaluate to?

      I'm not seeing it.. could you please explain?

      EDIT
      I'm guessing:
      False == 0, so if $key == FALSE then the elseif($key==0) will fire right?

      Perhaps it should be:
      elseif($key===0)
      to make sure that it's an integer returned.... not a bool or integer...

      Yeah.. that was it... THanks for that Laser.... good eyes

      But I'm still not seeing how i can reduce the count() calls... or are you just saying that there's no other option once it's past the other two....

      [EDIT]
      I updated the code for PHP 5 using array_slice, PHP4 using array_splice, and PHP3 using the normal $array[$key] = '' methods. Even though PHP4/5 support both array_s(p)lice....

        But I'm still not seeing how i can reduce the count() calls... or are you just saying that there's no other option once it's past the other two....

        For example:

        function removeElement($item, $array)
        {
        	if (($key = array_search($item, $array)) !== false)
        	{
        		if ($key == 0)
        		{
        			// Remove the first element
        			array_shift($array);
        		}
        		elseif ($key < (count($array)-1)) // Not first or last item
        		{
        			// A fairly simple process of removing the middle element
        			$arr1 = $arr2 = $array;
        			array_splice($arr1, 0, ($key+1));
        			array_splice($arr2, $key);
        			$array = array_merge($arr1, $arr2);
        		}
        		else
        		{
        			// Remove the last element
        			array_pop($array);
        		}
        	}
        	return $array;
        }

          Very true... I see it now. Thanks!!

            i am amazed by you guyz..are u all pro or wht..well i guess u all should be..i was like struggling to understand the coding and u all like its nothing~~..

            god i am amazed..

            btw..i'm not yet fully understand the coding..i really need to understand that first then only i can tell how it works or not with the rest of my coding...i'm dammmn one slow learner..

              I just realised - we're dealing with numerically indexed arrays, so there's no reason to use array_splice() where array_slice() will do.

              // remove item from array
              function removeElement($item, $array)
              {
              	// find the key of $item in $array
              	if (($key = array_search($item, $array)) !== false)
              	{
              		if ($key == 0) // first element
              		{
              			array_shift($array);
              		}
              		elseif ($key < (count($array) - 1)) // not first or last element
              		{
              			$array = array_merge(
              				array_slice($array, 0, $key), // part of array before item
              				array_slice($array, $key + 1) // part of array after item
              				);
              		}
              		else // must be last element
              		{
              			array_pop($array);
              		}
              	}
              	// return the array with the item removed
              	// if the item is not found, the array is returned unmodified
              	return $array;
              }

              i am amazed by you guyz..are u all pro or wht..well i guess u all should be..i was like struggling to understand the coding and u all like its nothing~~..

              I am a student too (from the island down south), all you need is more practice and experience.
              The PHP Manual at php.net is invaluable.

              By the way, since this is your FYP, I should say that the algorithm you outlined to compare URLs is potentially very slow (effectively an O(n**2) time algorithm). If the URL lists are in sorted order (e.g. place items in sorted order when creating list), you can merge them with a single pass (think O(n) time) through each list, assuming you create another list to hold the result. Even if you have to sort, sorting should take O(n log n) time, which is still faster than your current algorithm.

                really?than i am more amazed..

                actually i'm doing some sort of meta-search..my query will be sent to google, yahoo and altavista..the results from all these will be manipulate so that i got no redundant results where i use URL to compare since it is the only element that is unique... since i only retrieve first page of the result.. i got like 10 results each search engine.. all these will need to be filtered...

                each result i make it into array of 10 yahoo_res[1..10], alta_res[1..10], google_res[1..10] etc... also i have each result's URL that also in array.. yahoo_URL[1...10 <--corresponding to the results.. yahoo_res[$i] should be match with yahoo_URL[$i]

                so then i come out with the index array that have associative dimensional elements... i'm not really sure this kind of array relevent to my project but since i need to use the URL so i was like..ok fine juz use it..

                or may be should i use simpler kind of array???...can i do that to make this thing easier??? :o ... sorry.. i really am right now like this :queasy: to all these stuff..huhuuu~

                  i'm totally at lost here..really need help..please guysss~~
                  actually my problem still not resolved...
                  the workaround didnt work for me..
                  i change the array structure instead..i do array_merge and compare each element..so now i got only one multidimensional array...but unfortunately i got

                  Fatal error: Maximum execution time of 30 seconds exceeded in d:\webserv\www\stage2\testing_combine2.php on line 142

                  i know it must be the loop..or the searching process...

                  my array looks like:
                  $g_res[$i] = array($g_URL[$i],$g_arr[$i],'Google');
                  $y_res[$i] = array($y_URL[$i],$y_arr[$i],'Yahoo');
                  $a_res[$i] = array( $a_URL[$i],$a_array[$i],'Altavista');

                  each of these from within different loops in order to populate g_res,y_res,a_res...
                  e.g.
                  $g_res[1]= array('www.petronas.com.my','the desc of the result','Google');
                  so..i'm comparing the URL part..$g_URL,$y_URL,$a_URL

                  here's the pseudo...
                  1st search_values:
                  if found same url
                  put in other array[search_values]
                  unset(current_array) //delete the current element
                  keep searching if count($res)>0 //there's still result to be searched..
                  if found same again..
                  array_push(array[search_values],'Yahoo')
                  (at the end of the process..the results with the same URL will be deleted..leaving smaller number of result..

                  if same URL not found
                  call the function again with next search_values..

                  cont to 2ndURL:.....

                  $res = array_merge($g_res, $y_res, $a_res);
                   $counter = count($res);
                  $i=0;
                  $x=0;
                  filter($res,$x,$i,$counter);
                               function filter($res,$x,$i,$counter){
                  		$search_values=$res[$x][0];
                  		while($i<$counter){
                  		if(strpos($res[$i][0], $search_values)!==FALSE){
                  			//$new_array[$search_values]=$res[$i];
                  			//unset($res[$i]);
                  			$res = array_values($res);
                  			$counter--;
                  				if(($counter == 0) == FALSE ){
                  					filter($res, $x,$i++,$counter); //same $search_values 
                  				}
                  				/*
                  				else{
                  					$x++;
                  					$i=0;
                  					$counter = count($res);
                  					filter($res,$x,$i,$counter); //new $search_values
                  				}*/
                  			}
                  			}
                  
                  		echo count($res);
                  
                  }

                  help me here please...my presentation is no more than 2 weeks time..huuwaa~~~ 😕 at the same time :queasy:

                    i gues nobody could help me with these..i've made some correction ..but still got bugss and error...

                    i've got a numerical multidimesional array now..and i need to filter out the similar element based on URL comparing..could u all help me with this??

                    my array looks like:
                    $g_res[$i] = array($g_URL[$i],$g_arr[$i],'Google');
                    $y_res[$i] = array($y_URL[$i],$y_arr[$i],'Yahoo');
                    $a_res[$i] = array( $a_URL[$i],$a_array[$i],'Altavista');

                    each of these from within different loops in order to populate g_res,y_res,a_res...
                    e.g.
                    $g_res[1]= array('www.petronas.com.my','the desc of the result','Google');
                    $g_res[2]=array('www.petronas.com.my/indexcorp.htm','the desc of the result','Google');
                    $y_res[1]=array('www.petronas.com','the desc','Yahoo);

                    and..same goes to others..each array can containes from 10 - 12 elements..in other words 10 -12 URL...
                    so..i'm comparing the URL part..$g_URL,$y_URL,$a_URL..to make it simple i just array_merge all those three..but still i couldnt do it rite..hhuuhuhu~~

                    editted from my previous post

                    i just need the right algorithm...which if goes where, which else goes where..or should i use loop..please really..really need help here~~... 😕

                    $res = array_merge($g_res,$y_res, $a_res); 
                    $counter = count($res); 
                    $i=$x=$j=0; 
                    //$i(couting res) $x(counting search_values) $j(counting same_res) 
                    $same_res[$j]=""; 
                    filter($res,$x,$i,$j,$counter,$new_array); 
                       function filter($res,$x,$i,$j,$counter,$new_array){ 
                          $search_values=$res[$x][0]; 
                          if(strpos($res[$i][0], $search_values)!==FALSE){ //found same 
                             if(($same_res[$j] == "")==TRUE){ 
                                $same_res[$j]=$res[$i]; 
                             }else 
                             { 
                                      array_push($same_res[$j],$res[$i][2]); 
                             } 
                          unset($res[$i]); 
                          $res = array_values($res); 
                          $counter--; 
                             if(($counter == 0) == FALSE ){ 
                                filter($res, $x,$i,$j,$counter,$same_res[$j]); //same $search_values 
                             } 
                    
                      }//if end 
                    
                                                else{//if the search_values is not same as the URL 
                         $x++; $j++; $i=0; 
                         $counter = count($res); 
                         filter($res,$x,$i,$j,$counter,$same_res[$j]); //with new $search_values 
                      }//else end 
                    
                    
                    
                       }//function end

                    helpp please

                      owh..i'm soryy..the $new_array as the parameter should be $same_res..

                      thx in advance

                        Write a Reply...