How to get past [@attributes] in SimpleXMLElement? [duplicate] How to get past [@attributes] in SimpleXMLElement? [duplicate] codeigniter codeigniter

How to get past [@attributes] in SimpleXMLElement? [duplicate]


The attributes() function is what you're looking for I think. For example

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

Also you might have to use this instead:

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

(not sure, try both)


$employee->custom_fields->custom_field; is an array, and you can foreach over it to get each name attribute and its corresponding value using 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;  }}

Or if you don't know in advance all the ones you'll need, load them into an array:

$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);