2011-07-22 58 views
8

這是我的代碼:爲什麼SimpleXML在我使用它時將數組更改爲數組的第一個元素?

$string = <<<XML 
<?xml version='1.0'?> 
<test> 
<testing> 
    <lol>hello</lol> 
    <lol>there</lol> 
</testing> 
</test> 
XML; 
$xml = simplexml_load_string($string); 
echo "All of the XML:\n"; 
print_r $xml; 
echo "\n\nJust the 'lol' array:"; 
print_r $xml->testing->lol; 

輸出:

All of the XML: 

SimpleXMLElement Object 
(
    [testing] => SimpleXMLElement Object 
     (
      [lol] => Array 
       (
        [0] => hello 
        [1] => there 
       ) 

     ) 

) 




Just the 'lol' array: 

SimpleXMLElement Object 
(
    [0] => hello 
) 

爲什麼它僅輸出[0],而不是整個陣列?我沒有明白。

回答

5

這是因爲你有兩個lol元素。爲了訪問第二你需要做的是:

$xml->testing->lol[1]; 

這會給你「那裏」

$xml->testing->lol[0]; 

會給你「你好」

孩子()的方法SimpleXMLElement會給你一個包含元素所有子元素的對象,例如:

$xml->testing->children(); 

會給你一個一個包含「測試」SimpleXMLElement的所有子對象的對象。

如果您需要迭代,你可以使用下面的代碼:

foreach($xml->testing->children() as $ele) 
{ 
    var_dump($ele); 
} 

有關於的SimpleXMLElement這裏的更多信息:

http://www.php.net/manual/en/class.simplexmlelement.php

+0

這是否幫助您解決您的問題? –

+1

@Yattatron這並不回答問題的「原因」。如你所說,'$ lol [1]'=「there」和'$ lol [0]'=「hello」,爲什麼'print_r($ lol)'不能打印出'0 =>「hello」, 1 => 「有」'?? – chiliNUT

+0

@chiliNUT你有一個非常有用的觀點。我會思考並稍微修改我的答案。 –

6

什麼@Yottatron建議是正確的,但不在所有情況下,如本示例所示:

如果您的XML會是這樣的:

<?xml version='1.0'?> 
<testing> 
    <lol> 
     <lolelem>Lol1</lolelem> 
     <lolelem>Lol2</lolelem> 
     <notlol>NotLol1</lolelem> 
     <notlol>NotLol1</lolelem> 
    </lol> 
</testing> 

的SimpleXML的輸出將是:

SimpleXMLElement Object 
(
[lol] => SimpleXMLElement Object 
    (
     [lolelem] => Array 
      (
       [0] => Lol1 
       [1] => Lol2 
      ) 

     [notlol] => Array 
      (
       [0] => NotLol1 
       [1] => NotLol1 
      ) 

    ) 

) 

,並通過編寫

$xml->lol->lolelem 

你會期望你的結果是

Array 
(
    [0] => Lol1 
    [1] => Lol2 
) 

但不是它,你會得到:

SimpleXMLElement Object 
(
    [0] => Lol1 
) 

$xml->lol->children() 

,你會得到:

SimpleXMLElement Object 
(
[lolelem] => Array 
    (
     [0] => Lol1 
     [1] => Lol2 
    ) 

[notlol] => Array 
    (
     [0] => NotLol1 
     [1] => NotLol1 
    ) 

) 

你需要的,如果你只想要lolelem的做什麼:

$xml->xpath("//lol/lolelem") 

這給了這個結果(不如預期的形狀,但包含正確的元素)

Array 
(
    [0] => SimpleXMLElement Object 
    (
     [0] => Lol1 
    ) 

    [1] => SimpleXMLElement Object 
    (
     [0] => Lol2 
    ) 

) 
0

就遇到了這個問題...

的Xpath可以是有點慢,所以你可以實現一個簡單的for循環同樣的效果。

for ($i = 0; $i < count($xml->testing->lol); $i++) { 
    print_r($xml->testing->lol[$i]); 
} 
1

你可能想要的是使用JSON編碼做什麼/解碼

$jsonArray = Json_decode(Json_encode($xml), true); 

與真實的參數,你可以調用,而不是使用 - >使用[名]

這樣一個例子將是:

$xml = file("someXmlFile.xml"); 
$jsonArray = Json_decode(Json_encode($xml), true); 
foreach(jsonArray['title'] as $title){ 
    Do something with $titles 
} 

如果你有超過1個元素,它通常會放在@attributes中,如果元素ha的屬性。這可以通過使用:$title = $title['@attributes']

希望它可以幫助。

1

啊是的,我記得簡單的XML幾乎在我的腦海中解決數組問題 請嘗試下面的代碼。它會給你一個LOL元素的數組,或者,如果你只有一個LOL元素,它也會返回一個數組。

的,主要的好處是,你可以這樣做的foreach($笑爲$元素),它仍然會在一個單一的工作(或0上)LOL要素。

<?php 
$string = <<<XML 
<?xml version='1.0'?> 
    <test> 
    <testing> 
     <lol>hello</lol> 
     <lol>there</lol> 
    </testing> 
    </test> 
XML; 

$xml = simplexml_load_string($string); 
echo "<pre>"; 
echo "All of the XML:\n"; 
print_r($xml); 

echo "\n\nJust the 'lol' array:\n"; 

$test_lols = $xml->testing->children();    
$childcount = count($test_lols); 
if ($childcount < 2) { 
    $lol = array($test_lols->lol);     
} 
else { 
    $lol = (array) $test_lols; 
    $lol = $lol['lol']; 
    } 

print_r($lol); 
?> 
相關問題