2012-09-02 75 views
3

目標:使用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 
            ) 
          ) 
        ) 
      ) 
    ) 
) 

謝謝大家這麼多的幫助。浪費了許多小時的時間四處搜尋,嘗試了我所遇到的一切和我能想到的一切 - 我想我最好揮舞我的小白旗。 。:(

+0

參考的問題是:從SimpleXML的訪問@attribute(HTTP: //stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) – hakre

回答

1

$employee->custom_fields->custom_field;是一個數組,你可以通過foreach來獲得每個name屬性及其對應的value使用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); 
+0

正是我在找的東西。我嘗試過之前的屬性(),但由於某種原因,我一直在收到錯誤。我顯然錯過了一些東西。謝謝,邁克爾! – glassfish

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) { 

(不知道,嘗試兩者)

+0

謝謝Tech163。 – glassfish