I have this code:
$people=array();
$i=0;
foreach ($xml->xpath('//person') as $character) {
if ($character->status!="Active"){
$people[$i]['fullname']=(string)$character->fullname;
$people[$i]['status']=(string)$character->status;
$i++;
}
}
It creates an array with numeric keys, based on the value of $i. However, I don't actually want that, I want the "fullname" string to be the key but I can't work out how to dynamically assign the key. I was trying things like:
$people[(string)$character->fullname]=>(string)$character->status;
but this just throws errors. I can't work out how to create keys based on variables. Can anyone help, please?
再试一次,但是用=
,而不是=>
:
$people[ (string) $character->fullname ] = (string) $character->status;
You only use =>
in the Array definition. Otherwise just use bog-standard assignment:
$people[$character->fullname] = $character->status;
You don't need the casts, as you already have strings. Even if you didn't, you could simply rely on the dynamic typing to convert them as needed on output.
$people[$character->fullname] = (string)$character->status;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.