2012-10-31 3 views
1

내 PHP 간단한 XML 개체에 문제가 있습니다.xml to php 배열 문제

나는이 내 로그에 다음과 같은 XML 데이터

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [title] => test 
      [scanner] => Data 
      [id] => WordData 
      [position] => 1800 
      [error] => false 
      [num] => 6 
     ) 

    [subpod] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [title] => Surnames 
         ) 

        [plaintext] => Common (US population: 650 people, white: 63% | black: 35%) 
        [img] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (

            [alt] => Common (US population: 650 people, white: 63% | black: 35%) 
            [title] => Common (US population: 650 people, white: 63% | black: 35%) 
            [width] => 349 
            [height] => 18 
           ) 

         ) 

       ) 

      [1] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [title] => Cities 
         ) 

        [plaintext] => Littleton Common (Massachusetts, 2789 people) 
        [img] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (

            [alt] => Littleton Common (Massachusetts, 2789 people) 
            [title] => Littleton Common (Massachusetts, 2789 people) 
            [width] => 287 
            [height] => 18 
           ) 

         ) 

       ) 

      [2] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [title] => Buildings 
         ) 

        [plaintext] => Rotunda on Woolwich Common (London, Greater London) 
        [img] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (

            [alt] => Rotunda on Woolwich Common (London, Greater London) 
            [title] => Rotunda on Woolwich Common (London, Greater London) 
            [width] => 356 
            [height] => 18 
           ) 

         ) 

       ) 
       .....more simpleXmlElement object 
     ) 
) 

내 PHP는 varible $xmlObj = SimpleXMLElement Object하지만이 때

if (is_array($xmlObj->subpod)){ 
    echo 'is array';  
}else { 
    echo 'not array'; 
} 

출력이 '하지 배열'항상 내가 내 코드를 원하는 다음 에코 '는 배열입니다'

나는 $xmlObj->subpod이 배열이라고 생각했습니다. $xmlObj->subpod[0]->plaintext을 테스트하면 실제로 문자열이 반환됩니다. 이 문제를 해결하는 방법을 모르겠습니다. 누구든지 도와 줄 수 있습니까?

+0

'var_dump ($ xmlObj-> subpod); – AlienWebguy

+0

따옴표를 사용하는 사람은 어디에 있습니까? –

답변

2

var_dump($xmlObj->subpod) 인 경우 여전히 SimpleXMLElement 개체라는 것을 알 수 있습니다. 그러나 여전히 배열 인 것처럼 호출 할 수 있습니다. (클래스 문서를하지 않는 경우에도 클래스가 ArrayAccess를 구현하는 것을 암시하는 this example 후 노트를 참조하십시오.)

이 구조를 확인하는 올바른 방법은 SimpleXMLElement::count()을 사용하는 것입니다 :

if ($xmlObj->subpod->count() > 0) { ... } 

편집 : 귀하의 질문에 출력은 아마도 print_r에서 오히려 도움이되지 않는 경우가 있습니다. 일반적으로 디버깅에 더 유용하기 때문에 var_dump을 사용해보십시오. 여기서주의해야 할 점은 SimpleXMLElement 객체의 전체 중첩 된 객체 구조를 보지 못한다는 것입니다.

2

인사이드 SimpleXMLElement Object 모든 것이 SimpleXMLElement Object이고, print_r과 var_dump는 가끔 어레이에 관한 거짓말을합니다. 이들은 탐색 가능한 객체이지만, 그것들이 배열 인 것처럼 대부분의 연산을 수행 할 수 있습니다. 순수 배열을 고집한다면,이 노드를 배열로 캐스트 할 수 있습니다 :

$subpod = (array)$xmlObj->subpod;