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:
list_type
list
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.
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:
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: