[英]search through array of strings for string match from another array php
我有一個問題,我有兩個txt文件,其中一個包含要搜索的內容的值,第二個包含我需要從中查找包含第一個txt文件中的信息的文本行的信息。 1. txt文件如下所示:
1461527867 29012520 2220097051 2596180150 29012516 2596180152 29012514
<?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文件中給出的編號的所有行,然后我需要切出此搜索字符串行的緯度和經度值。
有誰能夠幫助我?
我將給您一些代碼以幫助您入門。
// 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
需要幫助請叫我。
您可以結合使用SimpleXMLElement
, foreach
, file_get_contents
和preg_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)
希望這會有所幫助。
好的,首先在您的示例中,您的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
嘗試這樣的事情。
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.