0

I write a program in PHP and now I learn assosiated arrays theme. This my cration and what is the program in it:

    <?PHP
 out("test starts");
 $people[] = array (
            "name" => "Brook",
            "age" => "42"
                 );
 $people[] = array (
            "name" => "Peter",
            "age" => "18"
                 );

 foreach($people as $person)
  out($person=>["name"] . " - " . $person=>["age"]);

/*
 I wanna this:
 Brook - 42
 Peter - 18

*/

 var_dump($people);
 out("test ends");


 function out($what) {
  echo $what . "<br>\n";
 }
?>

The var_dump is show me the array creation is good but the trouble is at foreach.

shilovk
  • 11,718
  • 17
  • 75
  • 74
Mr. Brooks
  • 33
  • 1
  • 8

3 Answers3

3

Your problem is most probably in this: out($person=>["name"] . " - " . $person=>["age"]);

instead use out($person["name"] . " - " . $person["age"]);

Khaldoun Nd
  • 1,436
  • 12
  • 23
0

Assigning:

$name = array("key1" => value1,
"key2" => value2);

Accessing:

echo $name["key1"];
echo $name["key2"];
Matthias Bö
  • 449
  • 3
  • 12
0

You have to remove =>. To access array properties you need to use square brackets $person["name"] (for objects use ->).

I found a very detailed answer about this topic here: How can I access an array/object?

Ebby
  • 514
  • 3
  • 9