-2

I keep getting this error. Can someone please elaborate on what the solution might be?

PHP Notice: Use of undefined constant name - assumed 'name'

<?php foreach ($replacement_array as &$replacement_array) { ?>
    <option value="<?php echo $replacement_array[number]?>">
    <?php echo $replacement_array[name]?></option>
<?php } ?>

Original array code:

$replacement_array[] = array('name'=>'1) 200W High Bay (400W Subst.)', 'name_2'=>'200W High Bay', 'name_3'=>'High Bay', 'number'=>'1', 'replacement_cal'=>'200', 'life'=>'100000');
$replacement_array[] = array('name'=>'2) 80W High Bay (150W Subst.)', 'name_2'=>'80W High Bay', 'number'=>'2', 'name_3'=>'High Bay', 'replacement_cal'=>'80', 'life'=>'50000');
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
Seymour
  • 1
  • 5

1 Answers1

1

You need to make those array keys into strings. Like this:

<?php foreach ($replacement_array as &$replacement_array) { ?>
    <option value="<?php echo $replacement_array['number']?>">
    <?php echo $replacement_array['name']?></option>
<?php } ?>

Otherwise they are parsed as if they are code or global functions etc, but obviously there aren't any.

AmazingDreams
  • 3,136
  • 2
  • 22
  • 32
  • Thankyou - this has been helpful. Now I feel stupid as I did the single quotes with another part of the code... which fixed. I missed this one though. Thanks again :) – Seymour Oct 12 '18 at 15:32
  • Hi there, Sorry for bumping up an old thread, but the comments here suggest single quotes, which doesn't seem to make a difference. Here is the code: $replacement_array[] = array('name'=>'1) 200W High Bay (400W Subst.)', 'name_2'=>'200W High Bay', 'name_3'=>'High Bay', 'number'=>'1', 'replacement_cal'=>'200', 'life'=>'100000'); Here is the error: Notice: Use of undefined constant name - assumed 'name' – Seymour Feb 19 '19 at 13:55