You want to assign multiple elements to an array in one step, but you don't want the first index to be 0.
Instruct array() to use a different index using the => syntax:
$fruits = array(1=>'Apples', 'Grapes', 'Bananas', 'Oranges');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array(1=>'Apples', 'Grapes', 'Bananas', 'Oranges'); // use foreach to loop through the array echo "<p><b>Start the index at 1 for the array.</b></p>"; foreach ($fruits as $key => $value) { echo "$key: $value<br />"; } // Use negative number $fruits2 = array(-1=>'Apples', 'Grapes', 'Bananas', 'Oranges'); echo "<p><b>Use negative number.</b></p>"; foreach ($fruits2 as $key => $value) { echo "$key: $value<br />"; } // Use multiple => $fruits3 = array(1=>'Apples', 'Grapes', 10=>'Bananas', 'Oranges'); echo "<p><b>Use multiple =>.</b></p>"; foreach ($fruits3 as $key => $value) { echo "$key: $value<br />"; } // Mix numeric and string keys $fruits4 = array(1=>'Apples', 'Grapes', 'yellow'=>'Bananas', 'Oranges'); echo "<p><b>Mix numeric and string keys</b></p>"; foreach ($fruits4 as $key => $value) { echo "$key: $value<br />"; } ?> </p> </body> </html> |