duanna1407 2013-08-06 08:21
浏览 66
已采纳

PHP in_array函数无法正常工作

I have this code that tries to do selection on every new element to put into an array ($desArray). It is hoped to not put an element that has the same value as one of the existing elements in the array.

The checking job is supposed to be done by in_array function, which checks if there already is the same title in $rss_array. So the array_push will only be done with the unique ones.

<?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>';
}
?>

What I'm asking is the in_array thing, It doesn't work and gives me this: Notice: Undefined index: (followed by the rss_array[title], repeatedly) Then the results displayed, but the duplicates keep showing up.

I tried to do this:

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

It dismisses the notice thing, but still doesn't dismiss the duplicates. For record, these Google, Bing and Free codes worked very well separately. But when I try to unite the results by removing the duplicates, this "stuckness" happens. How do I solve this? Thank you.. :)

  • 写回答

3条回答 默认 最新

  • 普通网友 2013-08-06 08:46
    关注

    Maybe you should create a rss_titles helper array like this

    $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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址