沃梦达 / 编程问答 / php问题 / 正文

如何从php数组中删除SimpleXMLElement对象

How to remove SimpleXMLElement Object from php array(如何从php数组中删除SimpleXMLElement对象)

本文介绍了如何从php数组中删除SimpleXMLElement对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用simplexml_load_filephp函数读取了XML文件。

我已将XML数据存储到php数组中,但在某些数组中其显示数组为XML[2-1] 当我打印XML数据时,它显示正常,但当我将XML值赋给php数组时,它显示为数组中上述XML对象

XML

<data>
    <request>
        <type>City</type>
        <query>Anand, India</query>
    </request>
    <current_condition>
        <observation_time>08:55 AM</observation_time>
        <temp_C>28</temp_C>
        <temp_F>82</temp_F>
        <weatherCode>113</weatherCode>            
        <weatherDesc>Sunny</weatherDesc>
        <windspeedMiles>9</windspeedMiles>
        <windspeedKmph>14</windspeedKmph>
        <winddirDegree>63</winddirDegree>
        <winddir16Point>ENE</winddir16Point>
        <precipMM>0.0</precipMM>
        <humidity>23</humidity>
        <visibility>10</visibility>
        <pressure>1014</pressure>
        <cloudcover>0</cloudcover>
    </current_condition>
        <weather>
            <date>2012-12-22</date>
            <tempMaxC>33</tempMaxC>
            <tempMaxF>91</tempMaxF>
            <tempMinC>18</tempMinC>
            <tempMinF>64</tempMinF>
            <windspeedMiles>11</windspeedMiles>
            <windspeedKmph>17</windspeedKmph>
            <winddirection>ENE</winddirection>
            <winddir16Point>ENE</winddir16Point>
            <winddirDegree>63</winddirDegree>
            <weatherCode>113</weatherCode>
            <weatherDesc>Sunny</weatherDesc>
            <precipMM>0.0</precipMM>
        </weather>
        <weather>
            <date>2012-12-23</date>
            <tempMaxC>34</tempMaxC>
            <tempMaxF>92</tempMaxF>
            <tempMinC>18</tempMinC>
            <tempMinF>65</tempMinF>
            <windspeedMiles>9</windspeedMiles>
            <windspeedKmph>15</windspeedKmph>
            <winddirection>ENE</winddirection>
            <winddir16Point>ENE</winddir16Point>
            <winddirDegree>64</winddirDegree>
            <weatherCode>113</weatherCode>
            <weatherDesc>Sunny</weatherDesc>
            <precipMM>0.0</precipMM>
        </weather>
        <weather>
            <date>2012-12-24</date>
            <tempMaxC>33</tempMaxC>
            <tempMaxF>92</tempMaxF>
            <tempMinC>18</tempMinC>
            <tempMinF>64</tempMinF>
            <windspeedMiles>9</windspeedMiles>
            <windspeedKmph>14</windspeedKmph>
            <winddirection>NE</winddirection>
            <winddir16Point>NE</winddir16Point>
            <winddirDegree>45</winddirDegree>
            <weatherCode>113</weatherCode>
            <weatherDesc>Sunny</weatherDesc>
            <precipMM>0.0</precipMM>
        </weather>
    </data>

PHP:

$child=$xml->children();
foreach($xml->children() as $child)
{               
    foreach($child->children() as $child_t)
    { 
        $weather_arr[$child_t->getName()]=$child_t;
        echo $child_t->getName() . ": " . $child_t . "<br />";   

    }
}

echo "<pre>".html_entity_decode(print_r($weather_arr,true))."</pre>";

推荐答案

我已使用以下代码解决了问题。

function simplexml_to_array($xmlobj) {
    $a = array();
    foreach ($xmlobj->children() as $node) {
        if (is_array($node))
            $a[$node->getName()] = simplexml_to_array($node);
        else
            $a[$node->getName()] = (string) $node;
    }
    return $a;
} 


foreach($xml->children() as $child)
{
    $arr=simplexml_to_array($child );
    echo "<pre>".html_entity_decode(print_r($arr,true))."</pre>";
}

这篇关于如何从php数组中删除SimpleXMLElement对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从php数组中删除SimpleXMLElement对象

基础教程推荐