I have an array to display the age like this:
$list = [
['name' => 'Quan', 'age' => '20'],
['name' => 'Jyri', 'age' => '30'],
['name' => 'Jani', 'age' => '250']
];
Then, in HTML part, I retrieve them using foreach.
<?php foreach($list as $list) { ?>
<h4><?php echo $list['name']; ?></h4>
<p><?php echo $list['age']; ?></p>
<?php }?>
The codes run successfully, I get the result out. Now I try to create a function to say my name out loud like this. Below is the codes in PHP part.
function sayName($name = 'John') {
echo "Good morning, $name.";
}
Below is html part. This runs successfully
<?php sayName($list[1]['name']);?>
Then I combine them both together like this (html part).
<?php foreach($list as $list) { ?>
<h4><?php echo $list['name']; ?></h4>
<p><?php echo $list['age']; ?></p>
<?php };
sayName($list[1]['name']);?>
The error I get is this.
Notice: Undefined offset: 1 in ..\Playground\ Good morning, . PHP Notice: Undefined offset: 1 in ..\Playground\
Please tell me how to fix this. Thank you. Here is the full codes: PHP Playground.
P/s: my question is not duplicate to this question since in my question, I have initialized the array.