繁体   English   中英

PHP in_array函数无法正常工作

[英]PHP in_array function does not work properly

我有这段代码尝试对要放入数组的每个新元素进行选择( $desArray )。 希望不要将元素的值与数组中现有元素之一的值相同。

检查工作应该由in_array函数完成,该函数检查$rss_array是否已经存在相同的标题。 因此, array_push将仅使用唯一的array_push来完成。

<?php

/* GOOGLE */

function cURL_google($url, $ref, $p) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if ($p) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
    return $result;
} else {
    return '';
}
}

if (isset($_GET['keyword'])) {
$keyword_google = $_GET['keyword'];
} else {
echo 'Wrong!';
}

$cseNumber = 'AAAAA';
$key = 'AAAAAP';
$rss_array = array();
$desArray = array();

function _json_decode($file) {
    if (get_magic_quotes_gpc()) {
    $file = stripslashes($file);
    }
    return json_decode($file);
}
$file = cURL_google('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' .             $cseNumber . '&q=' . $keyword_google . '&siteSearchFilter=i&alt=json&start=1&num=3', 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword_google . '&siteSearchFilter=i&alt=json&start=1&num=3', null); // . removed from here
$feed = _json_decode($file, true);
foreach ($feed->items as $item) {
$rss_item = array(
    'kind' => $item->kind,
    'Title' => $item->title,
    'Url' => $item->link,
    'Description' => $item->snippet,
    'formattedUrl' => $item->formattedUrl,
);
array_push($desArray, $item->snippet);
array_push($rss_array, $rss_item);
}

/* BING */
if (isset($_GET['keyword'])) {
$rawkeyword = '%27' . $_GET['keyword'] . '%27';
$keyword = str_replace(' ', '%27', $rawkeyword);
//echo $keyword . '<br>';
} else {
echo 'Wrong!';
}

$key_bing = 'AAAAAA'; 
$root = 'https://api.datamarket.azure.com/Bing/Search/';
$search = $root . 'Web?';
$req = $search . 'Query=' . $keyword . '&$top=3&$format=json';

$ch = curl_init($req);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $key . ":" . $key_bing);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($ch);
$json = json_decode($resp);

foreach ($json->d->results as $item) {
$rss_item = array(
    'Title' => $item->Title,
    'Description' => $item->Description,
    'DisplayUrl' => $item->DisplayUrl,
    'Url' => $item->Url,
);
if (in_array($rss_item[$item->Title], $rss_array, TRUE)) {
    return false;
} else {
    array_push($desArray, $item->Description);
    array_push($rss_array, $rss_item);
}
}

/* FREE */
if (isset($_GET['keyword'])) {
$rawkeyword = $_GET['keyword'];
$keyword = str_replace(' ', '%20', $rawkeyword);
echo $keyword . '<br>';
} else {
echo 'Wrong!';
}
$rootkiri = 'http://www.faroo.com/api?q=';
$key_free = '&key=AAAAAA';
$rootkanan = '&start=1&length=3&l=en&src=web&f=json';
$req = $rootkiri . $keyword . $rootkanan . $key_free;

$ch_free = curl_init($req);
curl_setopt($ch_free, CURLOPT_TIMEOUT, 30);
curl_setopt($ch_free, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch_free, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch_free, CURLOPT_SSL_VERIFYPEER, false);

$resp_free = curl_exec($ch_free);
$json_free = json_decode($resp_free);
foreach ($json_free->results as $item) {
if (isset($item->kwic)) {
    $rss_item = array(
        'Title' => $item->title,
        'Description' => $item->kwic,
        'Url' => $item->url,
    );
    if (in_array($rss_item[$item->title], $rss_array, TRUE)) {
        return false;
    } else {
        array_push($desArray, $item->kwic);
    }
} else {
    return false;
}
if (in_array($rss_item[$item->title], $rss_array, TRUE)) {
    return false;
} else {
    array_push($rss_array, $rss_item);
}
}

/* DISPLAYING RESULT */
for ($i = 0; $i < count($desArray); $i++) {
    echo '<p>'
    . '<a href="' . $rss_array[$i]['Url'] . '" onclick="return false;">'
    . '<img src="images/positif.png" title="rate this positive" onclick="positif(this);">'
    . '<img src="images/negatif.png" title="rate this negative" onclick="negatif(this);">'
    . $rss_array[$i]['Title']
    . '</a>'
    . '<a href="' . $rss_array [$i]['Url'] . '" target="_blank">'
    . '<img src="images/open_new_tab.jpg" title="open in new tab">'
    . '</a>'
    . '<br/>'
    . '<span class="deskripsi">' . $rss_array [$i]['Description'] . '</span>'
    . '<hr/>'
    . '</p>';
}
?>

我要问的是in_array事情,它不起作用,并且给了我这个: Notice: Undefined index:随后重复rss_array [title])然后显示结果,但是重复显示。

我试图这样做:

if (in_array($item->title, $rss_array, TRUE)) {

它消除了注意事项,但仍不消除重复项。 作为记录,这些Google,Bing和Free代码分别运行良好。 但是,当我尝试通过删除重复项来统一结果时,就会发生这种“卡顿”。 我该如何解决? 谢谢.. :)

如果您要查找的是确保标题唯一,则应尝试此操作

更换

if (in_array($rss_item[$item->Title], $rss_array, TRUE)) {

if (in_array($item->Title, array_map(function($e) { return $e['Title']; }, $rss_array))) {

这解决了2个问题:

  1. $rss_item[$item->Title]可能应该是$rss_item['Title'] ,但这等于$item->Title
  2. 您不想在$rss_array搜索$rss_array $item->Title ,而是想在$rss_array的所有数组的'Title'键中搜索$item->Title ,这是array_map作用。

更多阅读:

您使用的参数不正确。

in_array()文档中:

If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

我怀疑那是不是你想要的。

请尝试以下操作:

if (in_array($item->title, $rss_array) == TRUE) {

//code

}

甚至:

if(in_array($item->title, $rss_array)){

希望这可以帮助!

也许您应该像这样创建一个rss_titles帮助器数组

$rss_array = array();
$rss_titles = array();

foreach(...) {
    if (in_array($item->title, $rss_titles)) {
        //return false;
        continue; // Maybe you dont want to return false but to continue...
    } else {
        // you only need to create $rss_item if the title is not in_array:
        $rss_item = array(
            'Title' => $item->title,
            'Description' => $item->kwic,
            'Url' => $item->url,
        );
        array_push($rss_array, $rss_item);
        array_push($rss_titles, $item->title);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM