2016-07-13 141 views
0

我有一個問題,我有兩個txt文件,其中一個包含要搜索的值,其次包含從哪裏找到包含信息的文本行的信息從第一個txt文件。 1. txt文件看起來像這樣:從另一個數組中搜索字符串數組php

1461527867 
 
29012520 
 
2220097051 
 
2596180150 
 
29012516 
 
2596180152 
 
29012514

  • txt文件看起來像這樣:
  • <?xml version="1.0" encoding="UTF-8"?> 
     
    <osm version="0.6" generator="CGImap 0.4.3 (797 thorn-02.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/"> 
     
    <bounds minlat="56.7042000" minlon="21.5789000" maxlat="56.7340000" maxlon="21.6328000"/> 
     
    <node id="29012520" visible="true" version="2" changeset="3098893" timestamp="2009-11-12T15:12:59Z" user="Abuls" uid="14113" lat="56.7183050" lon="21.6051939"/> 
     
    <node id="29012518" visible="true" version="2" changeset="3098893" timestamp="2009-11-12T15:12:59Z" user="Abuls" uid="14113" lat="56.7184563" lon="21.6161018"/> 
     
    <node id="29012516" visible="true" version="2" changeset="3100827" timestamp="2009-11-12T19:23:00Z" user="Abuls" uid="14113" lat="56.7180402" lon="21.6222515"/> 
     
    <node id="29012514" visible="true" version="2" changeset="3100827" timestamp="2009-11-12T19:23:00Z" user="Abuls" uid="14113" lat="56.7191410" lon="21.6324709"/>

    所以我需要找到在2.txt所有的行,其中nod id等於1. txt文件中給出的nummber,並且我需要刪除搜索到的字符串行的緯度和經度值。

    任何人都可以幫助我嗎?

    +0

    也就是說,我已經嘗試了這麼多的代碼選項,我無法理解的事情我在做什麼了 - 所有的東西我迄今發現的是,如果其他數組包含您正在搜索的完全相同的值,則不會有問題來比較數組。但在這種情況下,我正在搜索數組值部分包含我正在搜索的值的情況 –

    +0

    因此基本上,如果在2.txt的節點ID中找到來自1.txt的任何行,則檢索相應的緯度和經度值。對? –

    +0

    是的,這是正確的 –

    回答

    0

    嘗試這樣的事情。

    $handle = fopen("1.txt", "r"); 
    
    $xml = simplexml_load_file("2.xml") or die("Error: Cannot create object"); 
    
    if ($handle) { 
    
        while (($line = fgets($handle)) !== false) { 
    
         foreach ($xml as $key => $value) { 
    
          if ($value['id'] == trim($line)) { 
    
           echo "id: " . $value['id'] . " Lat => " . $value['lat'] . " " . "lon => " . $value['lon'] . "</br>"; 
          } 
         } 
        } 
    
        fclose($handle); 
    
    } else { 
        // error opening the file. 
    } 
    

    輸出

    id: 29012520 Lat => 56.7183050 lon => 21.6051939 
    id: 29012516 Lat => 56.7180402 lon => 21.6222515 
    id: 29012514 Lat => 56.7191410 lon => 21.6324709 
    
    +0

    非常感謝! 這工作正常 –

    +0

    不客氣。 :) –

    0

    我會給你一些小小的代碼讓你開始。

    // Step 1: Need to load the files: 
    // put the paths to your files in these two variables 
    $textFile = 'locations.txt'; 
    $xmlFilePath = 'data.xml'; 
    
    $idHandle = fopen($textFile, 'r'); 
    $dom = new DOMDocument(); 
    $dom->load($xmlFilePath); 
    $dom->preserveWhiteSpace = false; 
    
    $xpath = new DOMXPath($dom); 
    
    // Step 2: getting each line in the text file 
    while ($id = fgets($idHandle)) { 
        $id = (int) $id; 
        // Step 3: selecting all the nodes that have ids matching 
        // the id in the text file 
        $query = "//node[@id={$id}]"; 
        $list = $xpath->evaluate($query); 
    
        foreach ($list as $node) { 
        echo "Node: {$id}, Latitude: " . $node->getAttribute('lat') 
         . ", " . "Longitude: " . $node->getAttribute('lon') . "\n"; 
        } 
    } 
    

    我得到以下輸出:

    Node: 29012520, Latitude: 56.7183050, Longitude: 21.6051939 
    Node: 29012516, Latitude: 56.7180402, Longitude: 21.6222515 
    Node: 29012514, Latitude: 56.7191410, Longitude: 21.6324709 
    

    讓我知道你需要什麼。

    0

    您可以結合使用SimpleXMLElement,foreach,file_get_contentspreg_split來獲得您想要的結果。這是什麼意思:

    <?php 
         // GET THE CONTENTS OF THE TEXT FILE CONTAINING IDS 
         // AND BUNDLE IT INTO A VARIABLE FOR LATER USE. 
         $txtFileContent = file_get_contents(__DIR__ . "/ids.txt"); 
    
         // CONVERT THE CONTENTS OF THE TEXT FILE TO AN ARRAY 
         $arrTextData = preg_split("#[\n\r]#", $txtFileContent); 
    
    
         // CREATE A SIMPLE XML ELEMENT USING THE XML FILE. 
         $sXML   = new SimpleXMLElement(__DIR__ . "/xml_file_name.xml", 0, true); 
    
         // LOOP THROUGH ALL THE ELEMENTS IN THE CHILDREN LIST 
         // AND TRY TO MATCH EACH ELEMENT WITH EACH KEY ON THE $arrTextData ARRAY. 
         // THIS WOULD IMPLY A NESTED LOOP... ALSO BUILD AN ARRAY OF 
         // MATCHED-ELEMENTS WITH THE UNIQUE ID AS THE KEY IN THE PROCESS. 
         $arrMatchedElements = array(); 
    
         foreach($sXML->children() as $nodeKey=>$nodeData){ 
          /**@var SimpleXMLElement $nodeData */ 
          foreach($arrTextData as $iKey=>$ID){ 
           if($ID == $nodeData->attributes()->id){ 
            $arrMatchedElements[$ID] = $nodeData->attributes(); 
           } 
          } 
         } 
    
         var_dump($arrMatchedElements); 
         // THE var_dump(...) PRODUCES...    
         array (size=3) 
          29012520 => 
          object(SimpleXMLElement)[6] 
           public '@attributes' => 
           array (size=9) 
            'id' => string '29012520' (length=8) 
            'visible' => string 'true' (length=4) 
            'version' => string '2' (length=1) 
            'changeset' => string '3098893' (length=7) 
            'timestamp' => string '2009-11-12T15:12:59Z' (length=20) 
            'user' => string 'Abuls' (length=5) 
            'uid' => string '14113' (length=5) 
            'lat' => string '56.7183050' (length=10) 
            'lon' => string '21.6051939' (length=10) 
          29012516 => 
          object(SimpleXMLElement)[5] 
           public '@attributes' => 
           array (size=9) 
            'id' => string '29012516' (length=8) 
            'visible' => string 'true' (length=4) 
            'version' => string '2' (length=1) 
            'changeset' => string '3100827' (length=7) 
            'timestamp' => string '2009-11-12T19:23:00Z' (length=20) 
            'user' => string 'Abuls' (length=5) 
            'uid' => string '14113' (length=5) 
            'lat' => string '56.7180402' (length=10) 
            'lon' => string '21.6222515' (length=10) 
          29012514 => 
          object(SimpleXMLElement)[8] 
           public '@attributes' => 
           array (size=9) 
            'id' => string '29012514' (length=8) 
            'visible' => string 'true' (length=4) 
            'version' => string '2' (length=1) 
            'changeset' => string '3100827' (length=7) 
            'timestamp' => string '2009-11-12T19:23:00Z' (length=20) 
            'user' => string 'Abuls' (length=5) 
            'uid' => string '14113' (length=5) 
            'lat' => string '56.7191410' (length=10) 
            'lon' => string '21.6324709' (length=10) 
    

    您也可以使這更面向對象,像這樣:

    <?php 
         // GET THE CONTENTS OF THE TEXT FILE CONTAINING IDS 
         // AND BUNDLE IT INTO A VARIABLE FOR LATER USE. 
         $txtFileContent = file_get_contents(__DIR__ . "/ids.txt"); 
    
         // CONVERT THE CONTENTS OF THE TEXT-FILE TO AN ARRAY 
         $arrTextData = preg_split("#[\n\r]#", $txtFileContent); 
    
         // CREATE A SIMPLE XML ELEMENT USING THE XML FILE. 
         $sXML   = new SimpleXMLElement(__DIR__ . "/xml_file_name.xml", 0, true); 
    
         // LOOP THROUGH ALL THE ELEMENTS IN THE CHILDREN LIST 
         // AND TRY TO MATCH EACH ELEMENT WITH EACH KEY ON THE $arrTextData ARRAY. 
         // THIS WOULD IMPLY A NESTED LOOP... ALSO BUILD A MATCHED-ELEMENTS 
         // ARRAY IN THE PROCESS. 
         $arrMatchedElements = array(); 
    
         foreach($sXML->children() as $nodeKey=>$nodeData){ 
          /**@var SimpleXMLElement $nodeData */ 
          foreach($arrTextData as $iKey=>$ID){ 
           if($ID == $nodeData->attributes()->id){ 
            $tempPropsObj    = new stdClass(); 
            $tempPropsObj->id   = $nodeData->attributes() 
              ->id->__toString(); 
            $tempPropsObj->lon   = $nodeData->attributes()->lon->__toString(); 
            $tempPropsObj->lat   = $nodeData->attributes()->lon->__toString(); 
            $tempPropsObj->uid   = $nodeData->attributes()->uid->__toString(); 
            $tempPropsObj->user   = $nodeData->attributes()->user->__toString(); 
            $tempPropsObj->timestamp = $nodeData->attributes()->timestamp->__toString(); 
            $tempPropsObj->changeset = $nodeData->attributes()->changeset->__toString(); 
            $tempPropsObj->version  = $nodeData->attributes()->version->__toString(); 
            $tempPropsObj->visible  = $nodeData->attributes()->visible->__toString(); 
            $arrMatchedElements[$ID] = $tempPropsObj; 
           } 
          } 
         } 
    
         var_dump($arrMatchedElements); 
         // DISPLAYS... 
         array (size=3) 
          29012520 => 
          object(stdClass)[6] 
           public 'id' => string '29012520' (length=8) 
           public 'lon' => string '21.6051939' (length=10) 
           public 'lat' => string '21.6051939' (length=10) 
           public 'uid' => string '14113' (length=5) 
           public 'user' => string 'Abuls' (length=5) 
           public 'timestamp' => string '2009-11-12T15:12:59Z' (length=20) 
           public 'changeset' => string '3098893' (length=7) 
           public 'version' => string '2' (length=1) 
           public 'visible' => string 'true' (length=4) 
          29012516 => 
          object(stdClass)[5] 
           public 'id' => string '29012516' (length=8) 
           public 'lon' => string '21.6222515' (length=10) 
           public 'lat' => string '21.6222515' (length=10) 
           public 'uid' => string '14113' (length=5) 
           public 'user' => string 'Abuls' (length=5) 
           public 'timestamp' => string '2009-11-12T19:23:00Z' (length=20) 
           public 'changeset' => string '3100827' (length=7) 
           public 'version' => string '2' (length=1) 
           public 'visible' => string 'true' (length=4) 
          29012514 => 
          object(stdClass)[8] 
           public 'id' => string '29012514' (length=8) 
           public 'lon' => string '21.6324709' (length=10) 
           public 'lat' => string '21.6324709' (length=10) 
           public 'uid' => string '14113' (length=5) 
           public 'user' => string 'Abuls' (length=5) 
           public 'timestamp' => string '2009-11-12T19:23:00Z' (length=20) 
           public 'changeset' => string '3100827' (length=7) 
           public 'version' => string '2' (length=1) 
           public 'visible' => string 'true' (length=4) 
    

    希望這有助於有點.... GOOD LUCK ;-)

    0

    好首先在你的例子中你的XML代碼沒有結尾標籤</osm>,這在複製粘貼時可能只是一個錯誤。

    因爲你有一個XML文件,你不需要自己解析它,PHP提供了很多功能爲你做。

    據我所知,不可能檢查一個XML的值是否等於另一個值,所以你必須在兩個循環中檢查它,正如你可以在下面的例子中看到的。

    <?php 
    //Read the id's 
    $ids = file("txt.txt", FILE_IGNORE_NEW_LINES); 
    
    //Read the XML file 
    $xmlContent = file_get_contents("txt.xml"); 
    
    //Convert XML file to an object 
    $xmlObjects = simplexml_load_string($xmlContent); 
    
    foreach ($ids as $id) { 
        foreach ($xmlObjects->node as $node) { 
    
         //Check if id's are equal 
         if ($node["id"] == $id) { 
          echo "ID: ".$id; 
          echo " Latitude: ".$node["lat"]; 
          echo " Longidtude ".$node["lon"]; 
          echo "<br>"; 
         } 
        } 
    } 
    

    輸出:

    ID: 29012520 Latitude: 56.7183050 Longidtude 21.6051939 
    ID: 29012516 Latitude: 56.7180402 Longidtude 21.6222515 
    ID: 29012514 Latitude: 56.7191410 Longidtude 21.6324709 
    
    相關問題