php.whyme php.whyme - 1 month ago 5
PHP Question

How to Solve PHP Notice: Array to String conversion on line... in a 2D array

(newbie here) I have a php file that is trying to echo out a table displaying movie details and the message below appears everytime:

Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/Unit1/halfTermProjects/associate.php on line 59

Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/Unit1/halfTermProjects/associate.php on line 60

A table is echoed below the message that looks like this:
Table echoed

My Code is here (the error occurs when trying to echo the table at the end):

<?php

$filmNames[0]="Hunger Games";
$filmNames[1]="Finding Dory";
$filmNames[2]="Twilight";
$filmNames[3]="Perfect Pitch";
$filmNames[4]="Parent Trap";
$filmNames[5]="Nanny McPhee";
$filmNames[6]="Enchanted";
$filmNames[7]="Frozen";
$filmNames[8]="Bring it On";
$filmNames[9]="Princess Bride";
$filmNames[10]="Mary Poppins";

//Length [0][0] -> [0][10]//

$filmDetails[0][0]=102;
$filmDetails[0][1]=113;
$filmDetails[0][2]=103;
$filmDetails[0][3]=102;
$filmDetails[0][4]=106;
$filmDetails[0][5]=112;
$filmDetails[0][6]=93;
$filmDetails[0][7]=121;
$filmDetails[0][8]=122;
$filmDetails[0][9]=112;
$filmDetails[0][10]=114;

//Rating [1][0] -> [1][10]//

$filmDetails[1][0]=5;
$filmDetails[1][1]=2;
$filmDetails[1][2]=5;
$filmDetails[1][3]=2;
$filmDetails[1][4]=5;
$filmDetails[1][5]=5;
$filmDetails[1][6]=3;
$filmDetails[1][7]=2;
$filmDetails[1][8]=3;
$filmDetails[1][9]=2;
$filmDetails[1][10]=4;

$table = "" ;
$table = "<TABLE border = '1'> ";

$table = $table."<TR>";
$table = $table . "<TH>Film Name</TH>";
$table = $table . "<TH>Film Length</TH>" ;
$table = $table . "<TH>Film Rating</TH>" ;
$table = $table."</TR>";

for ($x = 0; $x<11; $x++){
$table = $table."<TR>";
$table = $table . "<TD>$filmNames[$x]</TD>";
$table = $table . "<TD>$filmDetails([0][$x])</TD>";
$table = $table . "<TD>$filmDetails([1][$x])</TD>";
$table = $table."</TR>";
}
$table = $table . "</TABLE>" ;
echo $table;
echo "<br>";


?>

Answer

I changed a little bit of your code.

<?php

$filmNames[0]="Hunger Games";
$filmNames[1]="Finding Dory";
$filmNames[2]="Twilight";
$filmNames[3]="Perfect Pitch";
$filmNames[4]="Parent Trap";
$filmNames[5]="Nanny McPhee";
$filmNames[6]="Enchanted";
$filmNames[7]="Frozen";
$filmNames[8]="Bring it On";
$filmNames[9]="Princess Bride";
$filmNames[10]="Mary Poppins";

//Length [0][0] -> [0][10]//

$filmDetails[0][0]=102;
$filmDetails[0][1]=113;
$filmDetails[0][2]=103;
$filmDetails[0][3]=102;
$filmDetails[0][4]=106;
$filmDetails[0][5]=112;
$filmDetails[0][6]=93;
$filmDetails[0][7]=121;
$filmDetails[0][8]=122;
$filmDetails[0][9]=112;
$filmDetails[0][10]=114;

//Rating [1][0] -> [1][10]//

$filmDetails[1][0]=5;
$filmDetails[1][1]=2;
$filmDetails[1][2]=5;
$filmDetails[1][3]=2;
$filmDetails[1][4]=5;
$filmDetails[1][5]=5;
$filmDetails[1][6]=3;
$filmDetails[1][7]=2;
$filmDetails[1][8]=3;
$filmDetails[1][9]=2;
$filmDetails[1][10]=4;

$table = "" ;
$table = "<TABLE border = '1'> ";

  $table = $table."<TR>";
    $table .= "<TH>Film Name</TH>";
    $table .= "<TH>Film Length</TH>" ;
    $table .= "<TH>Film Rating</TH>" ;
  $table = $table."</TR>";

for ($x = 0; $x<11; $x++){
    $table .= "<TR>";
        $table .= "<TD>".$filmNames[$x]."</TD>";
        $table .= "<TD>".$filmDetails[0][$x]."</TD>";
        $table .= "<TD>".$filmDetails[1][$x]."</TD>";
    $table .= "</TR>";
    }
$table .= "</TABLE>" ;
echo $table;
echo "<br>";

You cannot just use the variable between " ". It will better for printing it out with "HTML CODE".$variable."HTML CODE" .

Comments