0

I am sorry to ask simple question.

function generateArray( $start, $end)
{

 //do task
 return $arr
} 

example 

$start= '47_000008';
$end= '47_000011'

$arr= generateArray( $start, $end);

here element of array should be

arr[0]=47_000008'
arr[1]=47_000009'
arr[2]=47_000010'
arr[3]=47_000011'

i need a help to write the function

here, start and end value can be between 47_000001 to 47_999999

Thanks

3
  • Will the function need to work with 48_ as well? With 49_? Commented Sep 19, 2012 at 10:20
  • what will be the value of $start & $end parameter Commented Sep 19, 2012 at 10:23
  • here, start and end value can be between 47_000001 to 47_999999 Commented Sep 19, 2012 at 10:26

2 Answers 2

2

Assuming your string format is XX_XXXXX. First of all split the string into array, like

$startList = explode("_",$start);
$endList = explode("_",$end);

Now run a for loop to increment the last value of the start array and push it to a result array, like

$totalindex = $endList[1]-$startList[1];
$startvalue=$startList[1];
for($i=0;$i<$totalindex;$i++){
   $startvalue=$startvalue+$i;
   $newArray[$i]=$startList[0]."_".$startvalue;
}

Consider the error validation.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your response. it returning 47_8, 47_9, 47_10, 47_11 as out out. i need 6 digit after '_'
1

here my version for leading null

    function generateArray( $start, $end)
    {
        $startArray=explode("_",$start);
        $endArray=explode("_",$end);
        $arr=array();
        if(count($startArray)==2)
        {
            $laenge=strlen($startArray[1]);
            $startInt=(int)$startArray[1];
            $endInt=(int)$endArray[1];
            for($i=$startInt;$i<=$endInt;$i++)
            {
                $arrString=""+$i;
                while(strlen($arrString)!=$laenge)
                    $arrString="0".$arrString;
                $arr[]=$startArray[0]."_".$arrString;
            }
        }
        return $arr;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.