Page 1 of 1

Associative Array in php

Posted: Tue May 03, 2016 9:11 pm
by nokuthula
Hello

am required to use associative array that will print out the year of registration of each module as well as the module code or name
am getting an error syntax error

<?php
// put your code here
//displaying each modules with years
$year=array("Eup1501"=>"2010","Ict1541"=>"2010","Ict1513"=>"2011",
"Ict1514"=>"2012","Ict1512"=>"2012","Ict1521"=>"2012","Ict1531"=>"2012",
"Ict1532"=>"2013","Fac1501","2015","Ict2611"=>"2014","Ict2621","2014",
"Ict2613"=>"2016","Ict2642","2016");

foreach($year as $x => &x_value){
echo "Key=" .$x . ",Value=" .$x_value;
echo "<br";
}

?>

Re: Associative Array in php

Posted: Tue May 03, 2016 9:17 pm
by requinix
The error message would have clearly told you that the error was on

Code: Select all

foreach($year as $x => &x_value){
that line. Do you see anything wrong with it?

And there are multiple mistakes with the construction of the array.

Re: Associative Array in php

Posted: Tue May 03, 2016 10:46 pm
by Christopher
It helps to post the error message. Often the error message will tell you exactly what and where the error is.

Re: Associative Array in php

Posted: Thu May 05, 2016 12:47 pm
by nokuthula
Thank Christopher is running as expected now