目标:使用PHP / CodeIgniter,我需要在可用的数组中获取用户及其自定义字段名称和值的列表。如果我能够找到物品,我可以做我需要的。请参阅OUTPUT以及“//<<<<”的注释,以便您可以看到我被卡住了。我只是无法通过dang [@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
)
)
)
)
)
)
非常感谢你的帮助。在浪费了几个小时搜索周围,尝试我遇到的一切以及我能想到的一切 - 我想我最好挥动我的小白旗。 :(
答案 0 :(得分:1)
我认为属性()函数是您正在寻找的。例如
foreach ($employee->custom_fields->custom_field as $line) {
echo "[" . $line->attributes()->name . "]" . $line->value . "\n";
}
此外,你可能不得不使用它:
foreach ($employee->_data->custom_fields->custom_field as $line) {
(不确定,试试两者)
答案 1 :(得分:1)
$employee->custom_fields->custom_field;
是一个数组,您可以foreach
使用SimpleXMLElement::attributes()
来获取每个name
属性及其对应的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);