[nycphp-talk] Help with while loop
Ken Robinson
kenrbnsn at rbnsn.com
Mon Nov 27 16:09:07 EST 2006
At 03:43 PM 11/27/2006, Randal Rust wrote:
>Yeah, I know. This is the client's hair-brained idea. I told him I'd
>take a look. The problem all goes back to how they are handling their
>data.
>
>They are using LDAP for data storage. Each record has a 'key' value
>which they want to be unique. The key is generated from a combination
>of three user-input values, so it's not as easy as 'Browns.' In the
>current system, there is no check in place to ensure that the key is
>unique. In the new system, they want the script to generate the key
>and ensure its uniqueness.
I took your original code and modified it extensively. It now uses
recursion to get a unique value and I add that value to the array. I
put echo statements at the end for debuging purposes. I also
hardcoded the "$newteam". Have a look and see if this solves your problem:
<?php
$teams=array(
'Reds',
'Browns',
'Bengals',
'Cavaliers',
'Blue Jackets',
'Indians'
);
$unique=false;
//$newTeam=$_POST['newTeam'];
$newTeam = 'Browns';
function checkDuplicate($value,$teams,$cnt=1){
$chk = in_array($value,$teams);
if (!$chk) {
$teams[] = $value;
return array(true,$teams);
}
$nt = substr($value,0,-1) . $cnt++;
list ($chk,$teams) = checkDuplicate($nt,$teams,$cnt);
while (!$chk) {
$nt = substr($value,0,-1) . $cnt++;
list ($chk,$teams) = checkDuplicate($nt,$teams,$cnt);
}
return(array($chk,$teams));
}
list($unique,$teams) = checkDuplicate($newTeam,$teams);
echo '<pre>' . var_export($unique,true) . "\n";
echo print_r($teams,true) . '</pre>';
list($unique,$teams) = checkDuplicate($newTeam,$teams);
echo '<pre>' . var_export($unique,true) . "\n";
echo print_r($teams,true) . '</pre>';
?>
Ken
More information about the talk
mailing list