Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find elements from the group array in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 435
    Comment on it

    Hello Reader's! if you have set of arrays and you want to get all of them seperatly then the code below will help you.

    Let's say if you have

    [ [a], [b], [c], [a, b], [a, c], [b, c], [a, b, c] ]

    and you want this

     [a,b,c] 
    

    The code will go like this:-

    function powerSet($in,$minLength = 1) { 
       $count = count($in); 
       $members = pow(2,$count); 
       $return = array(); 
       for ($i = 0; $i < $members; $i++) { 
          $b = sprintf("%0".$count."b",$i); 
          $out = array(); 
          for ($j = 0; $j < $count; $j++) { 
             if ($b{$j} == '1') $out[] = $in[$j]; 
          } 
          if (count($out) >= $minLength) { 
             $return[] = $out; 
          } 
       } 
       return $return; 
    } 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: