开发者

php -- combining arrays

So I'm trying to write a function that does the following: I have about 20 or so 开发者_如何学PythonXML files (someday I will have over a hundred) and in the header of each file is the name of a person who was a peer review editor <editor role="PeerReviewEditor">John Doe</editor>. I want to run through the directory where these files are stored and capture the name of the Peer-Review-Editor for that file. I want to end up with an variable $reviewEditorNames that contains all of the different names. (I will then use this to display a list of editors, etc.)

Here's what I've got so far. I'm worried about the last part. I feel like the attempt to turn $editorReviewName into $editorReviewNames is not going to combine the individuals for each file, but an array found within a given file (even if there is only one name in a given file, and thus it is an array of 1)

I'm grateful for your help.

function editorlist()
{
    $filename = readDirectory('../editedtranscriptions');
    foreach($filename as $file)
    {
        $xmldoc = simplexml_load_file("../editedtranscriptions/$file");
        $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0");
        $reviewEditorName = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor']");

        return $reviewEditorNames[] = $reviewEditorName;
    }


}


I would put things more apart, that helps as well when you need to change your code later on.

Next to that, you need to check the return of the xpath, most likely you want to process only the first match (is there one editor per file?) and you want to return it as string.

If you put things into functions of it's own it's more easy to make a function to only do one thing and so it's easier to debug and improve things. E.g. you can first test if a editorFromFile function does what it should and then run it on multiple files:

/**
 * get PeerReviewEditor from file
 *
 * @param string $file
 * @return string
 */
function editorFromFile($file)
{
    $xmldoc = simplexml_load_file($file);
    $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0");

    $node = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor'][1]");
    return (string) $node[0];
}

/**
 * get editors from a path
 *
 * @param string $path
 * @return array
 */
function editorlist($path)
{
    $editors = array();
    $files = glob(sprintf('%s/*.xml', $path), GLOB_NOSORT);
    foreach($files as $file)
    {
        $editors[] = editorFromFile($file);
    }
    return $editors;
}


Just a little update:

function editorlist() {
  $reviewEditorNames = array(); // init the 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");

    // add to the array
    $result = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor']");
    if (sizeof($result) > 0) {
      $reviewEditorNames[] = (string)$result[0];
    }
  }

  // return the array
  return $reviewEditorNames;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜