如果另一个属性具有特定值,则仅获取属性

发布于 2024-10-28 15:26:18 字数 1146 浏览 3 评论 0原文

好的,关于 stackoverflow 的第一个问题。

我有以下 xml:

<movies>
  <movie>
    <cast>
      <person name="Tim Johnson" character="" job="Director"/> 
      <person name="Avril Lavigne" character="Heather (voice)" job="Actor"/> 
      <person name="Omid Djalili" character="Tiger (voice)" job="Actor"/>
      <person name="Karey Kirkpatrick" character="" job="Director"/>
    </cast>
  </movie>
</movies>

我像这样检索它:

<?php $xml_getinfo_result = new SimpleXMLElement(file_get_contents($tmdb_getinfo_result)); ?>

要获得演员阵容,我使用以下内容:

$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  $tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
  echo "<li>".$tmdb_actors."</li>";
$i++;
}

这给了我:

<li>Tim Johnson</li>
<li>Avril Lavigne</li>
<li>Omid Djalili</li>
<li>Karey Kirkpatrick</li>

但是,如果我只想显示工作为“演员”的人员,我需要做什么?

Ok, first question on stackoverflow.

I have the following xml:

<movies>
  <movie>
    <cast>
      <person name="Tim Johnson" character="" job="Director"/> 
      <person name="Avril Lavigne" character="Heather (voice)" job="Actor"/> 
      <person name="Omid Djalili" character="Tiger (voice)" job="Actor"/>
      <person name="Karey Kirkpatrick" character="" job="Director"/>
    </cast>
  </movie>
</movies>

I retrieve it like this:

<?php $xml_getinfo_result = new SimpleXMLElement(file_get_contents($tmdb_getinfo_result)); ?>

To get the cast, I use the following:

$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  $tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
  echo "<li>".$tmdb_actors."</li>";
$i++;
}

This gives me:

<li>Tim Johnson</li>
<li>Avril Lavigne</li>
<li>Omid Djalili</li>
<li>Karey Kirkpatrick</li>

But what do I need to do if I want to display only the persons whose job is "Actor"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Spring初心 2024-11-04 15:26:18

你可以这样做:

$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  $tmdb_job = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->job;
  if($tmdb_job == 'Actor'){
     $tmdb_name = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
     echo "<li>".$tmdb_name."</li>";
  }
$i++;
}

you can do this:

$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  $tmdb_job = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->job;
  if($tmdb_job == 'Actor'){
     $tmdb_name = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
     echo "<li>".$tmdb_name."</li>";
  }
$i++;
}
傻比既视感 2024-11-04 15:26:18

两种可能性:

第一种,获取所有数据并仅显示演员:

$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  if ($xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->job == "Actor") {
     $tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
     echo "<li>".$tmdb_actors."</li>";
  }
$i++;
}

第二种是仅解析作为演员的 并显示所有结果。

Two possibilities:

The first one, you get all data and you display only actor:

$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  if ($xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->job == "Actor") {
     $tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
     echo "<li>".$tmdb_actors."</li>";
  }
$i++;
}

The second one is to parse only <person> that are actor and display all result.

喜你已久 2024-11-04 15:26:18
$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  $job = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->job;
  if ($job == 'Actor') {
    $tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
    echo "<li>".$tmdb_actors."</li>";
    }
  $i++;
  }
$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
  $job = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->job;
  if ($job == 'Actor') {
    $tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
    echo "<li>".$tmdb_actors."</li>";
    }
  $i++;
  }
夜巴黎 2024-11-04 15:26:18

您可以使用 xpath

$actorNodes = $xml_getinfo_result->xpath( '//person[@job="Actor"]' );
foreach( $actorNodes as $actorNode )
{
    echo "<li>".$actorNode->attributes()->name."</li>";
}

You could use xpath:

$actorNodes = $xml_getinfo_result->xpath( '//person[@job="Actor"]' );
foreach( $actorNodes as $actorNode )
{
    echo "<li>".$actorNode->attributes()->name."</li>";
}
允世 2024-11-04 15:26:18

我会把它放在 foreach 语句中

foreach ($xml_getinfo_result->movies->movie->cast->person as $id => $person) {
    if ($person->attributes()->job === 'Actor') {
        echo '<li>' . $person->attributes()->name . '</li>';
    }
}

I'd throw it in a foreach statement

foreach ($xml_getinfo_result->movies->movie->cast->person as $id => $person) {
    if ($person->attributes()->job === 'Actor') {
        echo '<li>' . $person->attributes()->name . '</li>';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文