function simplexml_to_array($simplexml_obj, $array_tags = array(), $strip_white = 1) { if ($simplexml_obj) { if (count($simplexml_obj) == 0) { return $strip_white ? trim((string) $simplexml_obj) : (string) $simplexml_obj; } $attr = array(); foreach ($simplexml_obj as $k => $val) { if (!empty($array_tags) && in_array($k, $array_tags)) { $attr[] = simplexml_to_array($val, $array_tags, $strip_white); } else { $attr[$k] = simplexml_to_array($val, $array_tags, $strip_white); } } return $attr; } return FALSE; }
function wee_val_array_by_value($val, $wee) { if (is_array($val) || is_numeric($val)) { return $val; } if ($val != null && is_object($val)) { if (is_a($val, 'SimpleXMLElement')) { if (method_exists($val, 'children') && count($val->children()) > 0) { return simplexml_to_array($val->children()); } else { return simplexml_to_array($val); } } $return = object_to_array($val); if (count($return)) { return $return; } } weeError("{$val} not an array", E_USER_WARNING); return $val; }
public function simplexml_to_array($xml) { $ar = array(); foreach ($xml->children() as $k => $v) { $child = simplexml_to_array($v); if (count($child) == 0) { $child = (string) $v; } foreach ($v->attributes() as $ak => $av) { if (!is_array($child)) { $child = array("value" => $child); } $child[$ak] = (string) $av; } if (!in_array($k, array_keys($ar))) { $ar[$k] = $child; } else { if ($ar[$k][0]) { $ar[$k][] = $child; } else { $ar[$k] = array($ar[$k]); $ar[$k][] = $child; } } } return $ar; }