0

I'm a beginner to PHP and am currently working on re-coding a small site from Classic ASP (VB) to PHP, and I was wondering if there was a fairly direct 'translation' in taking this concept from one to the other language?

I know that nested foreach loops would do the job, yet, I am curious if a for outer loop, with a foreach inner loop would work?

Here's what I've attempted to do. For some reason, the variable $strMonth_Alpha does not return. Thanks for any leads.

PHP Code:

$strMonthList='01,January;02,February;03,March;04,April;05,May;06,June;07,July;08,August;09,September;10,October;11,November;12,December';
$arrMonthList=explode(';',$strMonthList); //create the outer array
    //print_r(array_values($arrMonthList));
    for ($ii=0; $ii<count($arrMonthList); $ii++) {
        $M=explode(',',$arrMonthList[$ii]); //create the inner array
        //print_r(array_values($M));
        $myArrayCount=0;
            foreach($M as $a => $item) {
                    if ($myArrayCount==0) {
                    $strMonth_Num=$M[$myArrayCount];
                    //echo '<BR>$strMonth_Num...'.$strMonth_Num.'<BR>';
                    }

                    if ($myArrayCount==1) {
                    $strMonth_Alpha=$M[$myArrayCount];
                    }else{
                    $strMonth_Alpha='';
            $myArrayCount=($myArrayCount+1);
                 }  
            }
    }//to next month
1
  • Lots of code that doesn't do anything. What result do you expect anyway? Commented Mar 1, 2018 at 23:21

2 Answers 2

1

Tried your code, nothing except formatting has been changed, just echoed the variable and I get value "December". Is this what you want ?

Just add this to the end of your code after last bracket.

echo $strMonth_Alpha;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. I realize that after many hours, I missed the obvious and put the echo $strMonth_Alpha within the 'false' section of the if/then statement. No wonder it would not print.
1

print($strMonth_Alpha) within your code or print any other variables to debug and see where things are changing. I just tried printing this after your if/else statement and I get:

print($strMonth_Alpha . '<br/>');

January
February
March
April
May
June
July
August
September
October
November
December

Know that you are overwriting $strMonth_Alpha per loop.

If the data such as 01,January is always in this format (i.e. an assumption that your code can safely make), then you can simplify your code to remove the second for loop.

$arrMonthList=explode(';',$strMonthList); //create the outer array
//print_r(array_values($arrMonthList));
for ($ii=0; $ii<count($arrMonthList); $ii++) {
    $M=explode(',',$arrMonthList[$ii]); //create the inner array
    echo($M[0] . "<br/>"); // num
    echo($M[1] . "<br/>"); // month name

}//to next month

2 Comments

Great approach! That is very clean. Regarding counting the elements of the array in the inner loop, could the use of the $a variable (in counting the array element) be just the same, in effect, as the counting variable ($myArrayCount), if the counting variable starts at zero? I've tested it, and the results are the same, but I wasn't quite sure about how it operated within PHP.
I'm not sure what you mean but you already are using the count here: count($arrMonthList) which counts the number of entries (months) in your list, right? Or did you mean something else?

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.