I wrote the following codes:

<?php

$nama = ('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' =>  'Kit')

sort $nama
reset($nama)
   
$i=1;
do
      {
      $i++;
      echo "The key is " . $i . "and the value is “ .$i <br />";
      }
while ($i<=5);	
Echo "newline";
 
?>

this error appears:

Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\xampp\htdocs\php_exercise\exercise3.php on line 4

Why is it?

Thanks.

Dani AI

Generated

As and already pointed out, the original parse error was a syntax problem (missing the array construct and semicolons). The later "Undefined offset" notices are a different issue: the array uses string keys ('a'..'e'), so expressions like $nama[1] do not exist and produce that notice.

Two practical ways to fix it:

  • Iterate the associative array so you get the real keys and values (this is what suggested with foreach). That approach preserves the key names and avoids index errors.
  • If numeric indexing is required, reindex the array first and then loop. Example using array_values and count:
$values = array_values($nama);
$len = count($values);
for ($i = 0; $i < $len; $i++) {
    echo "Index " . ($i + 1) . " => " . $values[$i] . "<br>";
}

Notes about sorting and keys: sort() will sort values and reindex the array numerically; asort() sorts by value but preserves the original keys; ksort() sorts by key. Choose the function that matches whether you need to keep the original keys or not. See the manual for details: array_values, sort, asort, ksort, foreach, count.

Small extra tips: do not hardcode loop bounds (use count()), enable full error reporting while developing, and prefer foreach for associative arrays unless you intentionally need numeric reindexing.

Recommended Answers

All 4 Replies

Your code is sloppy, lines 4, 6 and 7 are not terminated with a semi-colon. Line 4 is missing array . Line 13 has a wrong double quote.

Make the following changes to your code:
1. One line, write

$nama = array('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' =>  'Kit');

instead of

$nama = ('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' => 'Kit')

2. On line 6, write

sort($nama);

instead of

sort $nama

3. Online 7, write

reset($nama);

instead of

reset($nama)
<?php

$nama = array('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' => 'Kit');

//sort ($nama);
//reset($nama);
   
$i=1;
do
      {
      echo "The key is " .$i. " and the value is " .$nama[$i];
	  $i++;
	  }	  
while ($i<=5);	
Echo "newline";
 
?>

The following error appears:

Notice: Undefined offset: 1 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 1 and the value is
Notice: Undefined offset: 2 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 2 and the value is
Notice: Undefined offset: 3 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 3 and the value is
Notice: Undefined offset: 4 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 4 and the value is
Notice: Undefined offset: 5 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 5 and the value is newline

Any clue? Thanks.

Yes, the keys are not 1 to 5, but a to e.

<?php 
$nama = array('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' => 'Kit');
 
//sort ($nama);
//reset($nama);
 
foreach ($nama as $key => $value)
{
  echo 'The key is ' . $key . ' and the value is ' . $value . '<br/>\n';
}	  
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.