Php 在对象数组中循环
我想循环一个对象数组,从中获取Php 在对象数组中循环,php,arrays,object,Php,Arrays,Object,我想循环一个对象数组,从中获取id、category和name。 这是我的php代码。这是通过cURL请求从服务器获取xml内容,然后将其转换为对象数组。我不知道如何循环使用它并获得我想要的所有值 <?php $url = "http://wingz.info/daas3b/search?q=word&top=&user=xxxx&pwd=xxxx"; $ch = curl_init(); curl_setopt ($ch,
id、category
和name
。
这是我的php代码。这是通过cURL请求从服务器获取xml内容,然后将其转换为对象数组。我不知道如何循环使用它并获得我想要的所有值
<?php
$url = "http://wingz.info/daas3b/search?q=word&top=&user=xxxx&pwd=xxxx";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$response = ob_get_clean();
ob_end_clean();
$response_array = @simplexml_load_string($response);
这将列出您需要的信息,并希望向您展示一点有关遍历SimpleXML对象的信息:
// Get the list of objects from your XML content
$objects_array = $response_array->content;
// Loop through all of the objects
foreach($objects_array as $xml_object) {
echo '<br />' . xml_object->name; // The name
echo '<br />' . xml_object->category; // The category
$attributes = xml_object->attributes();
echo '<br />' . $attributes['id']; // The id
}
//从XML内容中获取对象列表
$objects\u array=$response\u array->content;
//循环遍历所有对象
foreach($objects\u数组作为$xml\u对象){
echo'
'.xml\u object->name;//名称
echo“
”。xml_object->category;//类别
$attributes=xml_object->attributes();
回显“
”。$attributes['id'];//id
}
如果有问题,请告诉我,我将编辑此答案
编辑:如果不确定每个对象是否具有id/名称/类别,则使用以下更健壮的代码:
$objects_array = $response_array->content;
// Loop through all of the objects
foreach($objects_array as $xml_object) {
echo '<br />' . (isset(xml_object->name) ? xml_object->name : ''); // The name
echo '<br />' . (isset(xml_object->category) ? xml_object->category : ''); // The category
$attributes = xml_object->attributes();
echo '<br />' . (isset($attributes['id']) ? $attributes['id'] : ''); // The id
}
$objects\u array=$response\u array->content;
//循环遍历所有对象
foreach($objects\u数组作为$xml\u对象){
回显“
”。(isset(xml\u对象->名称)?xml\u对象->名称:“”;//名称
回显“
”(isset(xml\u对象->类别)?xml\u对象->类别:“”;//类别
$attributes=xml_object->attributes();
回显“
”。(isset($attributes['id'])?$attributes['id']:'';//id
}
有关使用SimpleXML对象的更多信息,请查看。这将列出您要求的信息,并希望向您展示一些有关遍历SimpleXML对象的信息:
// Get the list of objects from your XML content
$objects_array = $response_array->content;
// Loop through all of the objects
foreach($objects_array as $xml_object) {
echo '<br />' . xml_object->name; // The name
echo '<br />' . xml_object->category; // The category
$attributes = xml_object->attributes();
echo '<br />' . $attributes['id']; // The id
}
//从XML内容中获取对象列表
$objects\u array=$response\u array->content;
//循环遍历所有对象
foreach($objects\u数组作为$xml\u对象){
echo'
'.xml\u object->name;//名称
echo“
”。xml_object->category;//类别
$attributes=xml_object->attributes();
回显“
”。$attributes['id'];//id
}
如果有问题,请告诉我,我将编辑此答案
编辑:如果不确定每个对象是否具有id/名称/类别,则使用以下更健壮的代码:
$objects_array = $response_array->content;
// Loop through all of the objects
foreach($objects_array as $xml_object) {
echo '<br />' . (isset(xml_object->name) ? xml_object->name : ''); // The name
echo '<br />' . (isset(xml_object->category) ? xml_object->category : ''); // The category
$attributes = xml_object->attributes();
echo '<br />' . (isset($attributes['id']) ? $attributes['id'] : ''); // The id
}
$objects\u array=$response\u array->content;
//循环遍历所有对象
foreach($objects\u数组作为$xml\u对象){
回显“
”。(isset(xml\u对象->名称)?xml\u对象->名称:“”;//名称
回显“
”(isset(xml\u对象->类别)?xml\u对象->类别:“”;//类别
$attributes=xml_object->attributes();
回显“
”。(isset($attributes['id'])?$attributes['id']:'';//id
}
有关使用SimpleXML对象的更多信息,请查看。是否将“id”和“name”属性存储在不同的级别?另外:检查它们是否设置了/提供了默认值很好,但是使用三元“$name=isset($thing->name)?$thing->name:”,可能会更简洁。不过这是个人喜好的问题。哦,对不起,是的,id在属性中。。。等等,我会编辑。。。三元
:?
运算符是我在实际代码中使用的一个运算符,但在发布答案时经常尝试避免,因为它会混淆代码。感谢您的时间和努力。这很有帮助。@J.A.Streich“id”和“name”属性不是存储在不同的级别吗?另外:检查它们是否设置了/提供了默认值很好,但是使用三元“$name=isset($thing->name)?$thing->name:”,可能会更简洁。不过这是个人喜好的问题。哦,对不起,是的,id在属性中。。。等等,我会编辑。。。三元:?
运算符是我在实际代码中使用的一个运算符,但在发布答案时经常尝试避免,因为它会混淆代码。感谢您的时间和努力。它帮助了很多。@J.A.Streich(!)解析错误:语法错误,第15Ah行出现意外的“$xml\u对象”(T\u变量),抱歉,现在编辑将“$objects\u数组->as”更改为“$objects\u数组as”健壮版本,以响应“J.A.Streich”健壮的代码。谢谢你帮了我很多忙。非常感谢。@Hiphop03199(!)解析错误:语法错误,第15Ah行出现意外的“$xml\u对象”(T\u变量),抱歉,现在编辑以将“$objects\u数组->as”更改为“$objects\u数组as”健壮版本,以响应“J.A.Streich”健壮的代码。谢谢你帮了我大忙。非常感谢。@Hiphop03199