Undefined index: why?

http://pastie.org/1386807

//Script works as it should but…

Notice: Undefined index on line 14

$machine = array();
foreach($brand_machines as $value) {
$machine[$value[‘list_type’]].= “<li><a href=\“find.php?name={$value[‘list’]}\”>{$value[‘name’]}</a></li>”;
}

i think it has to do with the part in bold… is there a better way to write that?

The bold is where your error is generated.
The error is generated when you request an item in the array, but that item identifier (index) does not exist.
From what I can gather about your script (the foreach) is that:

  • You are going through an array called $brand_machines.
  • You are not extracting the index and values of the array, only the values.
  • Each value in the array $brand_machines is also an array containing the following values:
  1. list_type
  2. list
  3. name
  • The $machine array is an empty array which should be populated by the foreach.

The part in bold basically first get the value of $value[‘list_type’] and then tries to find that as an index in $machines to add the value to.
What I would have done is at the line of: $machine = array();
is to rather use : $machine = array(‘the-first-list_type’ => ‘’ , ‘the-second-list_type’ => ‘’ , …);
Using this, you define the list_type index and then everything should be working. This will work as long as you know all the list_type(s) that can be used. There is however a more advanced way to dynamically add the index when it will be used, but I could not figure it out right now.

I hope this helps.
Jacotheron

the error message is telling you that $value[‘list_type’] does not exist.

$value[‘list_type’] does exist.

I’m creating an array with an dynamic key and appending values to it.

echo the value of $value[‘list_type’] just above your bolded line and post the value.

If a value is echoed like @Kalon suggested, try the following:
To be able to append values to a dynamic key, that key fist have to be created (dynamically or static).
Currently that key does not exist in the $machines array and you are trying to append to it.
Try adding the following before the bold part in the loop:

if(!isset($machines[$value['list_type']])){
    $machines[$value['list_type']] = '';
}

This will attempt to create the key in $machines when it does not exist. and then your code should be able to work.

It works. No notices thrown anymore:

foreach($brand_machines as $value) {
    if(!isset($machine[$value['list_type']])){
    $machine[$value['list_type']] = '';
}

    $machine[$value['list_type']] .= "<li><a href=\\"find.php?name={$value['list']}\\">{$value['name']}</a></li>";
}

So it throws the notice because that key doesn’t exist… it doesn’t exist so we create it here:

$machine[$value['list_type']] = ''

; <— but why isn’t the error thrown here? Is it the concatenation operator? It’s trying to attach something to something that doesn’t exist? If that is it, you have a sharp eye.

I like how a person from South Africa can help a guy out it America out at 5am… That’s collaboration, that’s why I love site point. I checked it out… it was the concatenation operator, Jacotheron. Looks like you were right too, Kalon. It was a single period that made the difference. Happy Holidays.

The $machines variable is empty in the first go. So accessing $machine[$value[‘list_type’]] will generate the warning every time a new $value[‘list_type’] is encountered. To avoid this warning, just initialize the variable before attempting to concatenate anything in it:

foreach($brand_machines as $value) {
	if(isset($machine[$value['list_type']])){
		$machine[$value['list_type']] = "";
	}
	$machine[$value['list_type']].= "<li><a href=\\"find.php?name={$value['list']}\\">{$value['name']}</a></li>";
}