Zend - The PHP Company




Arrays

Add Code


(minus - subtract ) arrays  

Type: code fragment
Added by: mindf
Entered: 04/01/2001
Last modified: 05/12/2008
Rating: *** (13 votes)
Views: 9336
In this simple function you send two arrays and it gives you an array with the operation A-B, elements on A that are not included on B.


<?php
function RestaDeArrays($vectorA,$vectorB)
         {
           
$cantA=count($vectorA);
           
$cantB=count($vectorB);
           
$No_saca=0;
           for(
$i=0;$i<$cantA;$i++)
                {
                 for(
$j=0;$j<$cantB;$j++)
                      {
                       if(
$vectorA[$i]==$vectorB[$j])
                           
$No_saca=1;
                      }

                 if(
$No_saca==0)
                    
$nuevo_array[]=$vectorA[$i];
                   else
                   
$No_saca=0;
                }

           return 
$nuevo_array;
         }

?>


Usage Example


$result=RestaDeArrays($arrayA,$arrayB);
         


Rate This Script





Search



This Category All Categories