SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    filter values from array

    Hi,

    I have another working function here and need some advise from some experts to make the final piece work.
    Code:
    <?
    function permutations($letters,$num){ 
        $last = str_repeat($letters{0},$num); 
        $result = array(); 
        while($last != str_repeat(lastchar($letters),$num)){ 
            $result[] = $last; 
            $last = char_add($letters,$last,$num-1); 
        } 
        $result[] = $last; 
        return $result; 
    } 
    function char_add($digits,$string,$char){ 
        if($string{$char} <> lastchar($digits)){ 
            $string{$char} = $digits{strpos($digits,$string{$char})+1}; 
            return $string; 
        }else{ 
            $string = changeall($string,$digits{0},$char); 
            return char_add($digits,$string,$char-1); 
        } 
    } 
    function lastchar($string){ 
        return $string{strlen($string)-1}; 
    } 
    function changeall($string,$char,$start = 0,$end = 0){ 
        if($end == 0) $end = strlen($string)-1; 
        for($i=$start;$i<=$end;$i++){ 
            $string{$i} = $char; 
        } 
        return $string; 
    } 
     
    $number = '1234';
    $digit = 2;
    $Array=permutations($number,$digit); 
    for($i=0 ; $i < count($Array) ; $i++) { 
     
    echo  $Array[$i] . "<BR>"; 
     
    } 
    ?>
    The function above works fine but how can I remove all the results from the same numbers (permutation)

    e.g : 12, 21,, then only show 12 .. same goes to the others like 24, 42 then only show 24 without any repetition of the same digit.

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    split each string into array of chars - str_split()
    sort each of the subarrays - sort
    array_unique each subarray
    implode each subarray

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the idea and this is what I got but is not generating what I want.

    Code:
    $abc=str_split($Array[$i]); 
    sort($abc);
    array_unique($abc);
    $xyz = implode(",", $abc);
    echo "$xyz";
    Results generated : 1,11,21,31,41,22,22,32,41,32,33,33,41,42,43,44,4

    But I am expecting something like this : 12,13,14,23,24,34 ...

    Can you please advise which part I have done wrong or missed ? Thank you.


    Full script for your reference :
    Code:
    <?
    function permutations($letters,$num){ 
        $last = str_repeat($letters{0},$num); 
        $result = array(); 
        while($last != str_repeat(lastchar($letters),$num)){ 
            $result[] = $last; 
            $last = char_add($letters,$last,$num-1); 
        } 
        $result[] = $last; 
        return $result; 
    } 
    function char_add($digits,$string,$char){ 
        if($string{$char} <> lastchar($digits)){ 
            $string{$char} = $digits{strpos($digits,$string{$char})+1}; 
            return $string; 
        }else{ 
            $string = changeall($string,$digits{0},$char); 
            return char_add($digits,$string,$char-1); 
        } 
    } 
    function lastchar($string){ 
        return $string{strlen($string)-1}; 
    } 
    function changeall($string,$char,$start = 0,$end = 0){ 
        if($end == 0) $end = strlen($string)-1; 
        for($i=$start;$i<=$end;$i++){ 
            $string{$i} = $char; 
        } 
        return $string; 
    } 
     
    $number = '1234';
    $digit = 2;
    $Array=permutations($number,$digit); 
    for($i=0 ; $i < count($Array) ; $i++) { 
     
    // echo  $Array[$i] . "<BR>"; 
     
    $abc=str_split($Array[$i]); 
    sort($abc);
    array_unique($abc);
    $xyz = implode(",", $abc);
    echo "$xyz";
    
    } 
    ?>

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, my advice was off.

    PHP Code:
    $new = array();
    for(
    $i=$i count($Array) ; $i++) {

        
    $abc=str_split($Array[$i]);
        
    sort($abc);
        
    $new[] = implode(""$abc);

    }
    $new array_unique($new); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •