【问题标题】:using php to find if a array value exist in a for loop使用php查找for循环中是否存在数组值
【发布时间】:2017-09-06 17:10:12
【问题描述】:

我有一个名为 $top 的数组,它由像这样的车辆模型组成 -

$top = array('tacoma', 'corolla', 'camry');

我想知道这些值是否存在于名为 $title 的 for 循环变量中,我正在使用它来获取 rss 提要。我试过使用 array_search 却没有这样的运气 -

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $c++;
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('date')->item(0)->nodeValue,
        );
    array_push($feed, $item);

}


for($x=0;$x<$limit;$x++) {
    $title = $feed[$x]['title'];
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $item = $feed[$x]['item'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    $desc = preg_replace('/[^A-Za-z0-9\-]/', ' ', $description);
    $title = strtolower($title);

    $find= array_search($top, $title);

    if ($find) {
        // insert all matches in database
    } else {
        echo 'nothing found';
    }

}

这是 $title 产生的一个示例 - 丰田花冠低里程 (chico) 4500 美元。我想使用数组 $top 确定标题中是否存在花冠。

这是我回显 $title 时得到的 -

2012 chevy camaro 45 周年纪念版 (chico/orland) $115002006 三菱 raider dura-cross 皮卡出售 (yuba city, ca.) $59002016 dodge ram 2500 (corning) $430002016 dodge ram 2500 (corning) $4300020062007 toyota corolla低英里(奇科)4500 美元等...所以我想要提取包含 $top 数组中的字符串之一的所有标题。

【问题讨论】:

  • 您是否在for 循环中应用了这个if-else?我无法看到。另外$top = array(tacoma, corolla, camry); 无效,必须是$top = array('tacoma', 'corolla', 'camry');。我们也不知道$feed 是什么?你能证明它的价值吗?
  • 抱歉,我更新了更多信息。是的,if-else 在 for 循环内
  • Yes the if-else is inside the for loop ?在哪里?正确添加。不是单独的逻辑
  • 你试过array_key_exists()。欲了解更多信息w3schools.com/php/func_array_key_exists.asp

标签: php arrays for-loop


【解决方案1】:

您需要更改旅游代码,如下所示(更改在评论中给出):-

<?php
$feed = [];//create an empty array variable
$top = array('tacoma', 'corolla', 'camry'); // predefine array
foreach ($rss->getElementsByTagName('item') as $node) {
    //$c++; not needed because no where used
    $feed[] = [ 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('date')->item(0)->nodeValue,
        ]; // assign value directly
}

foreach($feed  as $fee){ //use foreach it will take care of indexes
    $title = $fee['title'];
    $link = $fee['link'];
    $description = $fee['desc'];
    $item = $fee['item'];
    $date = date('l F d, Y', strtotime($fee['date']));
    $desc = preg_replace('/[^A-Za-z0-9\-]/', ' ', $description);
    $title = strtolower($title);

    $find= array_search($top, $title);

    if (count(array_intersect($top, explode(" ",$title)))>0) { 
        echo $title. " matched";
    } else {
        echo 'nothing found';
    }

}

【讨论】:

  • 谢谢,我得到“没有找到”循环大约 20 次但没有匹配项
  • @RyanD 告诉我$title.echo 的不同值,告诉我你得到了什么
  • 好的,我已将其添加到问题中
  • @RyanD 现在检查我的答案。我已经编辑了if-else 条件部分。请看一下
【解决方案2】:

您可以使用array_intersectcount

if (count(array_intersect($top, $title)) > 0) {
    // do something
} else {
    echo 'nothing found';
}

解释: 当两个数组的交集留下一个或多个值时,则存在,否则不存在。


注意:确保在 for 循环中每次都覆盖$title。如果您在for 之外使用if,则只有最后一个值成立!

【讨论】:

  • 谢谢,它只会在没有找到任何东西时产生一个值。不显示有任何相交
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2013-04-11
相关资源
最近更新 更多