繁体   English   中英

PHP通过简单的xml循环并以字符串的形式遍历数组中的值

[英]PHP loop through simple xml and strore value in an array as a string

我有一个PHP函数($ get_user_info)返回一个xml字符串。 我试图遍历xml并将$ user-> id的值添加到数组中。 这可以工作,但是会将$ user-> id作为对象添加到数组。 有什么办法只能生成一个值数组。 例如array(10,12,13等)?

这是我正在使用的一些示例代码:

$users = simplexml_load_string($get_user_info);
$user_ids = array();
foreach ($users->user as $user) {
//echo "$user->id";       // this echoes the value which is what I want
$user_ids[] = $user->id;  // this adds an object to the array
}

谢谢!

简单解析为string呢?

$user_ids[] = (string) $user->id;

int

$user_ids[] = (int) $user->id;

您应该将对象转换为所需的原始类型

所以

$user_ids[] = (int) $user->id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM