Notice: Undefined index: label - php

I have this annoying error:
ERR (3): Notice: Undefined index: label in /var/www/html/www.mysite.com/prod/app/code/core/Mage/Core/Model/Layout.php on line 367
Any idea?

Your array does not contain an item with the index 'label', so PHP spits out this error.
For example:
// Works fine.
$i = array();
$i['label'] = "My Value";
echo $i['label'];
But this however, which is what your code is doing, will not work.
$i = array();
$i['somerandomlabel'] = "My Value";
echo $i['label'); // Will fail, 'label' in $i is never set
Examine your code and make sure you actually set 'label' at some point in time.

Related

Referencing a dynamic variable?

for ($count = 1; $count <= 5; ++$count) {
$test = ${'node->field_aw_score_' . $count}[LANGUAGE_NONE][0]['value'];
echo $test;
}
Throws the error:
Notice: Undefined variable: node->field_aw_score_1
Notice: Undefined variable: node->field_aw_score_2
Notice: Undefined variable: node->field_aw_score_3
Notice: Undefined variable: node->field_aw_score_4
Notice: Undefined variable: node->field_aw_score_5
However, the variables do exist. I'm trying to reference:
$node->field_aw_score_1[LANGUAGE_NONE][0]['value']
then
$node->field_aw_score_2[LANGUAGE_NONE][0]['value']
etc. A dynamic variable. What am I doing wrong? Thanks.
Try this instead:
$node->{'field_aw_score_' . $count}

Uninitialized string offset while building array

I am getting the following error from the method presented below:
Notice: Uninitialized string offset: 5 in /path/to/file.php on line 30 Fatal error: Cannot access empty property in path/to/file.php on line 30
private function parse($xml, $index = '') {
echo count($xml->children()); //outputs 6
$count = 0;
foreach ($xml->children() as $key => $value) {
$this->$key[$count] = array();
$count++;
}
}
Any ideas why if I build an multi-dimensional in this way it results in an error?
If I change the assignment to:
$this->$key = array($count = > array());
This simply re-assigns the property each loop.
Thanks
Rich
Imagine you've got a string:
$string = 'abc`;
Doing substring access (which looks like array) will return you the character:
echo $string[2]; # c
Or you get your error when you're out of the index:
echo $string[3]; # null + warning
So now accessing a member of your object $this dynamically:
$this->$string[2]; # access $this->c
However this one breaks hardly:
$this->$string[3]; # access $this->null (not possible)
This gives you your fatal error of an empty property, a property with no name.
This explain what happens in your code, you have not told what you're trying to do so I hope this information will help you to continue with writing your parse function.
You should try to create the array before filling it.
I.e. $this->key = array();
That is, before looping through the XML elements.

Undefined index - strange?

I experience something strange with an undefined index..
$vatcode = 'U25';
echo $this->vatcode_ids['U25']."\n";
echo $this->vatcode_ids[$vatcode]."\n";
foreach($this->vatcode_account_ids as $id => $vatcode){
echo $vatcode."\n";
echo $this->vatcode_ids[$vatcode]; // undefined index
}
this returns:
681
681
U25
Notice: Undefined index: U25 in /var/www/.....php on line 64
I don't get it?!
From empty line printed before Notice massage i assume your $vatcode variable contains some ending new line character. If so it does not match any key in $this->vatcode_ids array.
You should use some trimming function as Dan Lee suggested in comments.

Undefined variable, but it is there

I've got an array, but when I try to use it, I get the Undefined variable notice.
Here's the relevant lines:
$varEvents = array();
...
if ($selectedResult) {
while ($row = mysql_fetch_assoc($selectedResult)) {
array_push($varEvents, $row['eventID']);
}
mysql_free_result($selectedResult);
}
...
print_r($varEvents);
if (is_array($varEvents)) {
if (count($varEvents) > 0) {
if (in_array($id, $varEvents)) {
$varRegistered = 1;
}
}
unset($varEvents);
}
and the result shows as:
Array ( [0] => 4 )
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 143
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 145
line 143: print_r($varEvents);
line 145: if (is_array($varEvents)) {
All relevant lines are in the same loop and I do get most of the results I expect, except $varRegistered never changes to 1 and that messes up my result.
It is most likely because of this line:
unset($varEvents);
You are unsetting the variable within the loop and next iterations don't find it again.

'Undefined' Notice while populating arrays

While populating an array with data from a SimpleXML call, PHP throws exception to what it believes as 'Undefined' keys, however, the output is actually correct.
$doc = new SimpleXmlElement($http_result, LIBXML_NOCDATA);
$result = array();
$x = 0;
foreach($doc->users->user as $item) {
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
print json_encode($result);
This actually outputs what I expect, i.e. {"user":[{"id":"4843977","name":"Test New User","email":"test#newuser.com"}]}
However, the following errors are also present, and I'm not totally sure why - this doesn't appear in 5.2.6 but does for 5.2.10
Notice: Undefined index: user in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined offset: 0 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 1 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 2 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
I think You must change just ".=" to "=" in lines:
$result['user'][$x]['id'] = $item->id;
$result['user'][$x]['name'] = $item->name;
$result['user'][$x]['email'] = $item->email;
You don't define what are $result['user'] and $result['user'][$x].
You need to instantiate them as array so you won't get that error.
$result['user'] = array();
foreach($doc->users->user as $item) {
$result['user'][$x] = array();
$x++;
}
For the undefined indexes in the fields, the problem is similar. You use ".=" when the variable doesn't exists yet. So you should instantiate it first with an empty string.
$result['user'][$x]['name'] = '';
You need to initialize the $result array first:
$result = array('user' => array());
And since you’re using the string concatenation and assignment operator .=, that would also apply to the $result['user'][$x] arrays:
foreach($doc->users->user as $item) {
$result['user'][$x] = array(
'id' => null,
'name' => null,
'email' => null
);
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
But that’s not necessary since you can also write it like this:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id,
'name' => $item->name,
'email' => $item->email
);
}
Edit    Since we elaborated that the attributes of $item are SimpleXMLElement objects too, $item->attr[0] is required to address the string value itself. Thus:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id[0],
'name' => $item->name[0],
'email' => $item->email[0]
);
}
It happens, because you're not just setting the array values, but you're concatenating to the current value:
$result['user'][$x]['id'] .= $item->id;
This line means "take the current value of $result['user'][$x]['id'] and add $item->id to it". The notice is then thrown, because the current value is not yet existent.
Amend the code to this
$result['user'][$x]['id'] = $item->id;
and you should be safe. No idea though, why 5.2.6 is not throwing the errors, maybe you should check with the error_reporting setting in the php.ini.

Categories