'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.
精彩评论