2010-12-15 2 views
1

나는 다음과 같은 간단한 XML 객체 파일의 한 예를 들어simplexmlobject의 PHP의 위치를 ​​얻는 방법을

위치 성 Boeree 2 0F 6 인,

[AuthorList] => SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [CompleteYN] => Y 
     ) 

    [Author] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => van Tricht 
        [ForeName] => M J 
        [Initials] => MJ 
       ) 

      [1] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => Nieman 
        [ForeName] => D H 
        [Initials] => DH 
       ) 

      [2] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => Bour 
        [ForeName] => L J 
        [Initials] => LJ 
       ) 

      [3] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => Boerée 
        [ForeName] => T 
        [Initials] => T 
       ) 

      [4] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => Koelman 
        [ForeName] => J H T M 
        [Initials] => JH 
       ) 

      [5] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => de Haan 
        [ForeName] => L 
        [Initials] => L 
       ) 

      [6] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [ValidYN] => Y 
         ) 

        [LastName] => Linszen 
        [ForeName] => D H 
        [Initials] => DH 
       ) 

     ) 
) 

지금 내가 출력 한 이름의 위치에 원하는을

사람이를 알고 있나요 (0은 시작 인덱스는)?

답변

1

for 루프를 사용하십시오. 이 authorList이 SimpleXML을 객체

입니다 $ 말할 수 있습니다
$authorsCount = count($authorList->author); 
$result=-1; 

for($i=0;$i<$authorsCount;$i++){ 
    if($authorList->author[$i]->LastName =="Boeree"){ 
$result=$i; 
break; 
} 

if($result==-1) echo "Boereee not found"; 
else echo "position LastName Boree is {$result} of {$authorsCount}"; 

편집 :

+0

감사 안녕 ...하지만, 난 아주 새로운 오전 PHP와 배열 기능을 대신 배열의 SimpleXML을 개체를 사용하는 편집 코드, 위의 배열에서 올바른 변수로 작성자를 얻는 방법을 알려주시겠습니까? – Thijs

+0

개체의 이름이 $ authorList이면 $ authors = $ authorList-> author와 함께 작동해야합니다. –

관련 문제