PHP:在具有相应索引的另一个foreach循环中显示foreach循环的值

时间:2017-09-18 12:29:40

标签: php arrays object foreach

这就是我的XML文档的外观:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <result name="response" numFound="111" start="0">
    <doc>
      <str name="id">http://www.ams.at/</str>
      <str name="title">Startseite - Arbeitsmarktservice Österreich</str>
      <str name="url">http://www.ams.at/</str>
      <str name="content">evölkerung zurück. 982.000 Personen ab 50 Jahren hatten zuletzt einen</str>
    </doc>
    <doc>
      <str name="id">http://www.ams.at/ueber-ams/medien/download-formulare</str>
      <str name="title">Download und Formulare - Arbeitsmarktservice Österreich</str>
      <str name="url">http://www.ams.at/ueber-ams/medien/download-formulare</str>
      <str name="content">Download und Formulare - Arbeitsmarktnen Kundmachung AMS-Richtlinien Impressum AGB Sitemap Die</str>
    </doc>
  </result>
  <lst name="highlighting">
    <lst name="http://www.ams.at/service-arbeitsuchende/download-formulare">
      <arr name="content">
        <str>Service für Arbeitsuchende Service für Unternehmen <em>Berufsinformation</em> & Weiterbildung Service für Partner</str>
      </arr>
    </lst>
    <lst name="http://www.ams.at/berufsinfo-weiterbildung/berufsinfo-broschueren">
      <arr name="content">
        <str><em>Berufsinformation</em>: Kataloge, Broschüren und mehr - Arbeitsmarktservice Österreich English Schrift</str>
      </arr>
    </lst>
  </lst>
</response>

目的是使用Solr显示PHP查询的搜索结果。 它显示Title and the Content

我有一个表格,我在 doc 节点上进行foreach循环,我得到titleurlcontent

但是我无法使用 content 字段,因为我只想要搜索查询的相关内容。所以我使用 Solr突出显示。不幸的是,我现在有$data->result->doc数据,但突出显示的内容不在result->doc

我需要什么: 当我有我的表并执行foreach循环以获得每个doc结果的条目时,我想要具有突出显示字段的相应内容。 doc->title[0] + highlighting->content[0]等等。

1 个答案:

答案 0 :(得分:0)

工作解决方案:

$xml = simplexml_load_string(xml_file);
$i = 0;
foreach($xml->result->doc as $doc) {
  foreach ($doc->str as $str) {
    $name = $str->attributes()->name;
    echo $name . ': ' . $str . PHP_EOL;
  }
  $lstContent = $xml->xpath("/response/lst[@name='highlighting']")[0]->lst[$i]->arr->str;
  echo 'NEW CONTENT: ' . $lstContent . PHP_EOL;
  echo PHP_EOL;
  $i++;
}