【发布时间】: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