3

目标:使用 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();
    }       
}
?>

控制器/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
                                    )
                            )
                    )
            )
    )
)

非常感谢大家的帮助。在浪费了许多小时四处寻找,尝试了我遇到的所有事情和我能想到的所有事情之后——我想我最好挥动我的小白旗。:(

4

2 回答 2

1

我认为 attributes() 函数是您正在寻找的。例如

foreach ($employee->custom_fields->custom_field as $line) {
    echo "[" . $line->attributes()->name . "]" . $line->value . "\n";
}

此外,您可能不得不改用它:

foreach ($employee->_data->custom_fields->custom_field as $line) {

(不确定,两个都试试)

于 2012-09-02T15:56:26.037 回答
1

$employee->custom_fields->custom_field;是一个数组,您可以foreach通过它来获取每个name属性及其对应的valueusing SimpleXMLElement::attributes()

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);
于 2012-09-02T15:57:06.190 回答