2011-07-30 5 views
1

이것은 연관 배열을 작성한 첫 번째 시간입니다. 누구든지 나를 도울 수 있으면 감사 할 것입니다.연관 배열 작성

기본적으로 XML 파일의 디렉토리를 반복하고 싶습니다. 특정 편집자가이 파일의 편집자인지 확인하고, 조회가 사실이라면 두 가지 정보를 가져 와서 각각의 경우에 대해 두 가지 정보로 연관 배열의 결과를 얻고 싶습니다. 편집자의 이름이 있습니다.

그래서 여기 내가 지금까지 가지고있는 작업은 다음과 같습니다

function getTitleandID($editorName) { 

    $listofTitlesandIDs = array(); 

    $filename = readDirectory('../editedtranscriptions'); 

     foreach($filename as $file) 
      { 

      $xmldoc = simplexml_load_file("../editedtranscriptions/$file"); 
      $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0"); 

    if ($editorName == $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor']/text()")) 
    { 
    $title = $xmldoc->xpath("//tei:teiHeader/tei:title[1]"); 
    $id = $xmldoc->xpath("//tei:text/tei:body/tei:div/@xml:id[1]"); 

    $listofTitlesandIDs[] = //I don't know what to do here 
    } 
    else 
    { 
    $listofTitlesandIDs = null; 
    } 
} 
return $listofTitlesandIDs 
} 

이 나는 ​​문제가 발생할 경우에 관한 것입니다. 예를 들어 두 개의 다른 키에 대한 값을 호출 할 수있는 연관 배열로 $listofTitlesandIDs을 가질 수있게하고 싶습니다. $listofTitlesandIDs['title']$listofTitlesandIDs[$id]

그렇습니다. 나는 당신이 제공 할 시간이 있다면 도움을 주셔서 감사합니다.

+0

분명히하십시오. 배열을 불러올 때 어떤 결과가 나옵니까? 이드, 타이틀 또는 저자 또는 모두? –

+0

다음과 같이 쓰면 : $ listofTitlesandIDs [] = array ("title"=> $ title, "id"=> $ id)'; – Jeff

+0

그러면 일정 시간에 ID와 제목을 찾을 수 없습니다. –

답변

0
$listofTitlesandIDs[$id] = $title; 

foreach 루프를 사용하여 배열을 반복해야합니다.

1

글쎄, 나는 이것이 아마추어의 결과로 조금 서투른 것이라고 확신하지만, 내가 원하는 결과를 내게 주었다. 이에

function getTitlesandIDs($EditorName) //gets titles and IDs for given author 
{ 
$list = array(); 
$filename = readDirectory('../editedtranscriptions'); 
foreach($filename as $file) 
{ 

    $xmldoc = simplexml_load_file("../editedtranscriptions/$file"); 
    $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0"); 

    $title = $xmldoc->xpath("//tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:title[1]"); 
    $id = $xmldoc->xpath("//tei:text/tei:body/tei:div/@xml:id"); 
    $editorName = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor']/text()") 

    if ($editorName[0] == "$EditorName") 
    { 
    $result = array("title"=>$title[0], "id"=>$id[0]); 

    $list[] = $result; 
    } 
} 
return $list; 
} 

I $list = getTitlesandIDs('John Doe') 함수를 호출하고, 각 인스턴스의 결합 배열의 제목과 ID 모두를 액세스 할 수있다. 좋아요 :

foreach ($list as $instance) 
    { 
     echo $instance['title']; 
     echo $instance['id']; 
    } 

어쩌면 누군가가 언젠가는 도움이 될 것입니다.이 기사를 더 우아하게 만드는 것에 대한 조언이 있다면 알려주세요.