1

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.

  • 1
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – treyBake Jul 03 '19 at 11:26
  • You overwrite `$list` in the loop. change it to `foreach ($list as $l)` and use `$l` instead of `$list` inside the loop. – Qirel Jul 03 '19 at 11:36

3 Answers3

1

You shouldn't use the array's name as the variable name for the value doring foreach. Instead of:

<?php foreach($list as $list) { ?>
        <h4><?php echo $list['name']; ?></h4>
        <p><?php echo $list['age']; ?></p>
<?php }?>

Try:

<?php foreach($list as $element) { ?>
        <h4><?php echo $element['name']; ?></h4>
        <p><?php echo $element['age']; ?></p>
<?php }?>
Andrew
  • 827
  • 2
  • 6
  • 14
  • Thanks. It works now. Moreover, back to my old combination, when I change it to sayName($list['name']); , it works but it shows Jani, the last person in the array. It's weird. – Winston Lycan Jul 03 '19 at 11:38
1

You are looping parent considering its child element means $list looping $list so it won't work,

<?php foreach ($list as $list1) {?>
<h4>
    <?php echo $list1['name']; ?>
</h4>
<p>
    <?php echo $list1['age']; ?>
</p>

<?php 
sayName($list1['name']);
}
?>

</body>

And if you will write inside loop it will work for every name,

Your updated snippet.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

Actually your array $list is overwrite with the same variable $list

If you print this array:

$list = [
    ['name' => 'Quan', 'age' => '20'],
    ['name' => 'Jyri', 'age' => '30'],
    ['name' => 'Jani', 'age' => '250']
];

this will return you this result:

Array
(
    [0] => Array
        (
            [name] => Quan
            [age] => 20
        )

    [1] => Array
        (
            [name] => Jyri
            [age] => 30
        )

    [2] => Array
        (
            [name] => Jani
            [age] => 250
        )

)

But, after this loop:

<?php foreach($list as $list) { ?>
        <h4><?php echo $list['name']; ?></h4>
        <p><?php echo $list['age']; ?></p>
<?php }?>

When you try to print this:

sayName($list[1]['name']);?>

This will definetly give you Notice for Undefined offset because if you check your array by using print_r, this will give you this result:

print_r($list);

Result:

Array
(
    [name] => Jani
    [age] => 250
)

So solution is that, call your function inside your loop and change the variable name of your value like:

<?php foreach($list as $value) { // change this line ?>
    <h4><?=sayName($value['name']);?></h4>    
<?php }?>
devpro
  • 16,184
  • 3
  • 27
  • 38