Jump to content

removing array


Alicia

Recommended Posts

Hi,

 

I have another working function here and need some advise from some experts to make the final piece work.


<?
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.

 

Link to comment
https://forums.phpfreaks.com/topic/188406-removing-array/
Share on other sites

It all depends.  Will all your numbers 2 digit numbers?  Do you want to keep the highest number or the lowest? 

for 2 digits and keeping the lowest digit something like this should work.

foreach($array as $digit)
{
if(isset($array[strrev($digit)]) && $digit < strrev($digit)) { unset($array[$strrev[$digit]); } 
}

Link to comment
https://forums.phpfreaks.com/topic/188406-removing-array/#findComment-995014
Share on other sites

try

<?php

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; 
} 
function my_remove($array){
foreach ($array as $k => $v){
	$c = str_split($v);
	sort($c);
	$c = implode('',$c);
	if ($v != $c) unset($array[$k]);
}
return array_values($array);
}
$number = '1234';
$digit = 2;
$Array=permutations($number,$digit); 
$Array = my_remove($Array);

for($i=0 ; $i < count($Array) ; $i++) { 

echo  $Array[$i] . "<BR>\n"; 

} 
?>

Link to comment
https://forums.phpfreaks.com/topic/188406-removing-array/#findComment-995053
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.