2011-07-30 22 views
1

これは私の最初の結合配列を構築することになるでしょう。誰かが私を助けることができれば、私は感謝しています。連想配列を構築する

基本的に、私はXMLファイルのディレクトリをループしたいと思います。あるエディタがこのファイルのエディタであるかどうかを知りたいのですが、クエリが真であれば、2つの情報を取得し、その2つの情報でアソシエート配列の結果をエディタの名前が見つかります。

だからここに私がこれまで持っているものです:

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を2つの異なるキーの値を呼び出すことができる連想配列として持たせたいと思っています。 $listofTitlesandIDs['title']$listofTitlesandIDs[$id]

だからそれです。私は、あなたが提供する時間があれば助けてくれて感謝しています。

+0

明らかにしてください:配列の呼び出しは何を返すべきですか? ID、タイトル、著者、またはそれらのすべて? –

+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']; 
    } 

多分これはいつか誰かを助けるでしょう - これをよりエレガントにするためのアドバイスがあれば教えてください。