目標:使用PHP/CodeIgniter,我需要獲取用戶及其自定義字段名稱和值在可用數組中的列表。如果我能找到物品,我可以做我需要的。請參閱OUTPUT附註「// < < <」,以便您可以看到我被卡住的位置。我無法越過當時的[@特性]。如何在SimpleXMLElement中過去[@attributes]?
我使用PHP連接到Redmine使用ActiveResource.php和他們內置的REST API。我想獲得所有用戶的列表,並獲得下面的OUTPUT。
型號/ Employees_model.php:
<?php
require_once ('ActiveResource.php');
class Employees_model extends ActiveResource {
var $site = 'http://user:[email protected]: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;
?>
OUTPUT:
Array
(
[0] => Employees_model Object
(
[id] =>
[site] => http://user:[email protected]: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] => [email protected]mple.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
)
)
)
)
)
)
謝謝大家這麼多的幫助。浪費了許多小時的時間四處搜尋,嘗試了我所遇到的一切和我能想到的一切 - 我想我最好揮舞我的小白旗。 。:(
參考的問題是:從SimpleXML的訪問@attribute(HTTP: //stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) – hakre