2013-06-04 2 views
0

XML 파일의 노드 값을 루핑하고 있지만 필요에 따라 출력을 얻을 수 없습니다. 아래 코드는 제가 작업하고있는 코드입니다.

PHP :PHP가 XML 출력을 배열로 읽어들입니다.

$xml = simplexml_load_file("file.xml") or die("Error: Cannot create object");   
$result = array(); 
foreach($xml->picture as $item) 
{ 
    $result[] = $item->logo; 
} 

echo '<pre>'; 
print_r($result); 
echo '</pre>'; 



전류 출력 :

Array 
(
    [0] => SimpleXMLElement Object 
     (
      [0] => img/a.jpg 
     ) 

    [1] => SimpleXMLElement Object 
     (
      [0] => img/b.jpg 
     ) 

    [2] => SimpleXMLElement Object 
     (
      [0] => img/c.jpg 
     ) 

    ... 
) 



원하는 출력 :

Array 
(
    [0] => a.jpg 
    [1] => b.jpg 
    [2] => c.jpg 

    ... 
) 
+0

내가 내 대답은 당신이 당신의 문제를 해결하는 데 도움 생각합니다. 질문을 받아들이도록 동의해야합니다. 답변을 수락하지 않으면 사람들이 귀하의 질문을 클릭하지 않습니다. – Brian

답변

0

확인이 링크 아웃 : here

function toArray(SimpleXMLElement $xml) { 
    $array = (array)$xml; 

    foreach (array_slice($array, 0) as $key => $value) { 
     if ($value instanceof SimpleXMLElement) { 
      $array[$key] = empty($value) ? NULL : toArray($value); 
     } 
    } 
    return $array; 
} 
0

배열에서 루프의 수를 지정, 당신의 코드는 다음과 유지 :

$xml = simplexml_load_file("file.xml") or die("Error: Cannot create object");   
$result = array(); 
$i = 0;//set a variable to loop throw the foreach 
foreach($xml->picture as $item) 
    { 
//assign the variable with the number of the loop in the disired array 
     $result[$i] = $item->logo; 
    } 

echo '<pre>'; 
print_r($result); 
echo '</pre>'; 
관련 문제