0

Hi I've created an array with a for loop inside. The array doesn't have any data inside it until it reaches the for loop. I wanted to add an associative array to what I've done so far. For example my array currently ouputs

[0] => Array
        (
            [0] => Version 5
            [1] => Feb-16
            [2] => gary
            [3] => 80
            [4] => P
        )

I would like it to have headings instead of numbers

[0] => Array
        (
            Version => Version 5
            Date => Feb-16
            Name => gary
            RandNum => 80
            Letter => P
        }

I'm not sure how i'd fit into my loop and how I could if my different columns these headings. Below is my current code. Which outputs the array at the top.

 for($i = 0; $i <= 3; $i++){

      for($j = 0; $j <= 4; $j++){

           if ($j == 0){
                $times_table[$i][$j]=  "Version 5" ;
           }
           else if ($j == 1){
                $cur_date = date("M-y", $currentdate);

                $currentdate = strtotime('+1 month', $currentdate);

                $times_table[$i][$j]= "<strong>" . $cur_date . "</strong>" ;
           }
           else{
                    $times_table[$i][$j]=  "gary" ;
           }
           if ($j == 3) {
                    $numbers = mt_rand(1, 100);
                    $times_table[$i][$j]= $numbers ;

           }
           if ($j == 4){

                    if($i == 0 || $i == 3)
                    {
                        $pay = "P";

                         $times_table[$i][$j]= $pay ;
                    }
                    else{
                        $int = "I";

                         $times_table[$i][$j]= $int ;
                    }
           }

      }

 }
2
  • Can you describe the final output of your multidimensionnal array that you wish for ? Commented Dec 10, 2015 at 12:34
  • @jiboulex I think they want "headings instead of numbers" which they have included the proposed output. Commented Dec 10, 2015 at 12:37

4 Answers 4

1

Try this..

for($i = 0; $i <= 3; $i++){

             for($j = 0; $j <= 4; $j++){

               if ($j == 0){

                $times_table[$i]['Version']=  "Version 5" ;
            }
                else if ($j == 1){
                $cur_date = date("M-y", $currentdate);

                $currentdate = strtotime('+1 month', $currentdate);

                $times_table[$i]['Date']= "<strong>" . $cur_date . "</strong>" ;


                }
                else{
                    $times_table[$i]['Name']=  "gary" ;
                }
                if ($j == 3) {
                    $numbers = mt_rand(1, 100);
                    $times_table[$i]['RandNum']= $numbers ;

                }
                if ($j == 4){

                    if($i == 0 || $i == 3)
                    {
                        $pay = "P";

                    $times_table[$i]['Letter']= $pay ;
                    }
                    else{
                        $int = "I";

                    $times_table[$i]['Letter']= $int ;

                    }
                }

            }

            }

Hope this will solve your problem.

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

1 Comment

Damn, you beat me to it. touché
1

In my opinion, your second for loop is not needed. You should know that you can create associative arrays like this :

<?php
$times_table = [];
$times_tables[] = [
    'Version' => 'Version 5',
    'Date' => 'Feb-16',
    'Name' => 'gary',
    'RandNum' => '80',
    'Letter' => 'P',
];

To match with your code :

<?php
$times_table = [];
for($i = 0; $i <= 3; $i++){
    $times_table[$i]['Version']=  "Version 5" ;
    $cur_date = date("M-y", $currentdate);
    $currentdate = strtotime('+1 month', $currentdate);
    $times_table[$i]['Date']= "<strong>" . $cur_date . "</strong>" ;
    $times_table[$i]['Name']=  "gary" ;
    $numbers = mt_rand(1, 100);
    $times_table[$i]['RandNum']= $numbers ;
    switch ($i) {
        case 0:
        case 3:
            $letter = 'P';
        break;
        default:
            $letter = 'I';
    }
    $times_table[$i]['Letter']= $letter;
}

I think this should do what you want in a cleaner way !

2 Comments

Thanks for your reply. Could I ask why the keys aren't in the first array. And why create the second array $times_tables
there is one and only array : $times_table, which is multidimensional
0
for($i = 0; $i <= 3; $i++){

             for($j = 0; $j <= 4; $j++){

               if ($j == 0){

                $times_table["Version"][$j]=  "Version 5" ;
            }
                else if ($j == 1){
                $cur_date = date("M-y", $currentdate);

                $currentdate = strtotime('+1 month', $currentdate);

                $times_table["Date"][$j]= "<strong>" . $cur_date . "</strong>" ;


                }
                else{
                    $times_table["Name"][$j]=  "gary" ;
                }
                if ($j == 3) {
                    $numbers = mt_rand(1, 100);
                    $times_table["RandNum"][$j]= $numbers ;

                }
                if ($j == 4){

                    if($i == 0 || $i == 3)
                    {
                        $pay = "P";

                    $times_table["Letter"][$j]= $pay ;
                    }
                    else{
                        $int = "I";

                    $times_table["Letter"][$j]= $int ;

                    }
                }

            }

            }

Comments

-1

You can create associative array like this:

$a=array("Version"=> "Version 5",
         "Date"=> "Feb-16",
         "Name" => "gary",
         "RandNum" => 80,
         "Letter" => "P"
        )

To access this array in for loop use $a['key']. For eg. to access version5, use $a['version'], to access Feb-16 use $a['Date'].

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.