php - 如何通过 SimpleXMLElement 中的 [@attributes]?

标签 php xml codeigniter simplexml activeresource

<分区>

目标:使用 PHP/CodeIgniter,我需要在可用数组中获取用户列表及其自定义字段名称和值。如果我能拿到元素,我就能做我需要做的事。请查看带有“//<<<”注释的输出,这样您就可以看到我卡在哪里了。我就是无法摆脱该死的 [@attributes]。

我正在使用 PHP 通过 ActiveResource.php 及其内置的 REST API 连接到 Redmine。我想获得所有用户的列表,并得到下面的输出。

模型/Employees_model.php:

<?php
require_once ('ActiveResource.php');

class Employees_model extends ActiveResource {
    var $site = 'http://user:password@localhost:8080/redmine/';
    var $element_name = 'user';
    var $request_format = 'xml';

    function __construct() {
        parent::__construct();
    }       
}
?>

controllers/employees.php:

public function employees () {
    $employees = new Employees_model();     
    $data['employeeList'] = $employees->find('all');        
    $this->load->view('customers/ajaxEmployees', $data);
}

查看/ajaxEmployees.php:

<?php
//I can get the following with no problem
echo $employee->id . "<br/>";
echo $employee->firstname . "<br/>";
echo $employee->lastname . "<br/>";

//This is where I'm stuck (see OUTPUT for [@attributes] and "// <<<" notes)
echo $employee->custom_fields->custom_field;
?>

输出:

Array
(
[0] => Employees_model Object
    (
        [id] => 
        [site] => http://user:password@localhost:8080/redmine/
        [element_name] => user
        [request_format] => xml
        [extra_params] => 
        [user] => 
        [password] => 
        [element_name_plural] => users
        [_data] => Array
            (
                [id] => 16
                [login] => jschmoe
                [firstname] => Joe
                [lastname] => Schmoe
                [mail] => joe@example.com
                [created_on] => 2012-08-24T01:58:21Z
                [last_login_on] => SimpleXMLElement Object
                    (
                    )
                [custom_fields] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [type] => array
                            )

                        [custom_field] => Array
                            (
                                [0] =>     SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => Quality Control //<<< I need this
                                                [id] => 2
                                            )
                                        [value] => 1 //<<< I need this to know that QualityControl = 1
                                    )
                                [1] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => Technical Contractor // <<< I need this
                                                [id] => 3
                                            )
                                        [value] => 0  // <<< I need this to know that TechnicalContractor = 0
                                    )
                                [2] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => Content //<<< I need this
                                                [id] => 4
                                            )
                                        [value] => 0  // <<< I need this to know Content = 1
                                    )
                            )
                    )
            )
    )
)

非常感谢大家的帮助。在浪费了很多时间四处寻找、尝试了我遇到的一切以及我能想到的一切之后——我想我最好挥动我的小白旗。 :(

最佳答案

$employee->custom_fields->custom_field; 是一个数组,您可以foreach 获取每个 name 属性及其使用 SimpleXMLElement::attributes() 对应的 value .

foreach ($employee->custom_fields->custom_field as $cf) {
  // Loop over the custom field nodes and read the name of each
  switch ($cf->attributes()->name) {
    // Load a variable on each matching name attribute
    // or do whatever you need with them.
    case "Quality Control":
      $quality_control = $cf->value;
      break;
    case "Technical Contractor":
      $technical_contractor = $cf->value;
      break;
    case "Content":
      $content = $cf->value;
      break;
  }
}

或者如果你事先不知道你需要的所有那些,将它们加载到一个数组中:

$employee_attrs = array();
foreach ($employee->custom_fields->custom_field as $cf) {
  // Add an array key of the name, whose value is the value
  $attrs[$cf->attributes()->name] = $cf->value;
}

var_dump($employee_attrs);

关于php - 如何通过 SimpleXMLElement 中的 [@attributes]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12233354/

相关文章:

c# - 反序列化 xml 返回空

php - 如何在 CodeIgniter 中取消链接(删除)图像

php - 在 CodeIgniter 上如何进行最简单的身份验证/授权

php - 如何使用 Codeigniter Select with sum if?

php - 如何每天轮换报价

php - PDO INSERT 不适用于 $_POST 变量

php - 在 phpFiddle.org 上运行良好的代码在 Web 服务器上崩溃

xml - 无法指定 OfficeApp manifest.xml 的要求集

php - 在数据库中保存多个值,在 PHP 中使用行

c# - 通过代码C#在xml中的特定位置添加注释