如何以不同方式动态访问数组对象

时间:2012-03-21 12:28:23

标签: php web

我正在解析数组的xml文档.. 如何访问这些数组对象并将其保存在新变量中? 这是一段解析

的PHP代码
$contents = file_get_contents('test.xml');
$xml = simplexml_load_string($contents);
print_r($xml);

这是正在解析的xml

<?xml version='1.0'?>
<document>
<txtmsg>
    <smstel>1234567</smstel>
    <smstxt></smstxt>
    <smsdir>Send</smsdir>
    <smstime>06/01/2010 7:54:48 am</smstime>
</txtmsg>
<txtmsg>
    <smstel>33333333</smstel>
    <smstxt>Test sms hhTes12222222</smstxt>
    <smsdir>Send</smsdir>
    <smstime>06/01/2010 7:54:48 am</smstime>
</txtmsg>

<Contacts>
    <conttime>06/01/2010 8:19:05 am</conttime>
        <cnt>
            <fn>Abc</fn>
            <ln>Def</ln>
            <cnttel>123456</cnttel>
            <cntmtel>3333333</cntmtel>
            <cntemail>abc@hotmail.com</cntemail>
        </cnt>
        <cnt>
            <fn>def</fn>
            <ln>ghi</ln>
            <cnttel>234234</cnttel>
            <cntmtel>2424</cntmtel>
            <cntemail>df@hotmail.com</cntemail>
        </cnt>
</Contacts>
</document>

这是输出。

SimpleXMLElement Object ( [txtmsg] => Array ( [0] => SimpleXMLElement Object ( [smstel] => 1234567 [smstxt] => SimpleXMLElement Object ( ) [smsdir] => Send [smstime] => 06/01/2010 7:54:48 am ) [1] => SimpleXMLElement Object ( [smstel] => 33333333 [smstxt] => Test sms hhTes12222222 [smsdir] => Send [smstime] => 06/01/2010 7:54:48 am ) ) [Contacts] => SimpleXMLElement Object ( [conttime] => 06/01/2010 8:19:05 am [cnt] => Array ( [0] => SimpleXMLElement Object ( [fn] => Abc [ln] => Def [cnttel] => 123456 [cntmtel] => 3333333 [cntemail] => abc@hotmail.com ) [1] => SimpleXMLElement Object ( [fn] => def [ln] => ghi [cnttel] => 234234 [cntmtel] => 2424 [cntemail] => df@hotmail.com ) ) ) ) 

如何单独访问xml的每个元素..如smstel,smstxt,smsdir等

6 个答案:

答案 0 :(得分:3)

也许我过度简化它,但循环

foreach($xml->txtmsg as $txtmsg) {
  echo $txtmsg->smstel;
  echo $txtmsg->smstxt;
  // more elements...
}

注意:使用XML时,有助于了解架构。意思是上面的例子特定于你提到的那些元素。尽管如此,它应该可以帮助您入门。

答案 1 :(得分:2)

(string)$xml->txtmsg->smstel

同样适用于params和list ..它既可以是对象属性,也可以是数组

答案 2 :(得分:2)

这是你能做到的最佳方式..

foreach($xml->txtmsg as $txtmsg) {
  echo $txtmsg->smstel;
  // more elements...
}

答案 3 :(得分:1)

echo $xml->txtmsg[0]->smstel; // 1234567
echo $xml->txtmsg[1]->smstel; // 3333333

答案 4 :(得分:1)

$smsTel = (string) $xml->txtmsg[0]->smstel; // 1234567

要将<txtmsg>部分转换为多维数组,您可以这样做:

$array = array();
foreach ($xml->txtmsg as $msg) {
  $array[] = array (
    'smstel' => (string) $msg->smstel,
    'smstxt' => (string) $msg->smstxt,
    'smsdir' => (string) $msg->smsdir,
    'smstime' => (string) $msg->smstime
  );
}
print_r($array);

/*
  Array
      (
          [0] => Array
              (
                  [smstel] => 1234567
                  [smstxt] =>
                  [smstel] => Send
                  [smstime] => 06/01/2010 7:54:48 am
              )
          [1] => Array
              (
                  [smstel] => 33333333
                  [smstxt] => Test sms hhTes12222222
                  [smstel] => Send
                  [smstime] => 06/01/2010 7:54:48 am
              )
      )
*/

答案 5 :(得分:1)

这是代码。 它会将SimpleXML Object转换为Array

function convertXmlObjToArr($obj, &$arr)
{
    $children = $obj->children();
    foreach ($children as $elementName => $node)
    {
        $nextIdx = count($arr);
        $arr[$nextIdx] = array();
        $arr[$nextIdx]['@name'] = strtolower((string)$elementName);
        $arr[$nextIdx]['@attributes'] = array();
        $attributes = $node->attributes();
        foreach ($attributes as $attributeName => $attributeValue)
        {
            $attribName = strtolower(trim((string)$attributeName));
            $attribVal = trim((string)$attributeValue);
            $arr[$nextIdx]['@attributes'][$attribName] = $attribVal;
        }
        $text = (string)$node;
        $text = trim($text);
        if (strlen($text) > 0)
        {
            $arr[$nextIdx]['@text'] = $text;
        }
        $arr[$nextIdx]['@children'] = array();
        convertXmlObjToArr($node, $arr[$nextIdx]['@children']);
    }
    return;
}  

您将获得此格式的输出

Array
(
    @name => books
    @attributes => array ( )
    @children => array
    (
        array
        (
            @name => novel
            @attributes => array ( author => John Doe )
            @children => array
            (
                array ( @name => title, @attributes => array ( ), @text => John's Novel )
            )
        )
    )
)