Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在foreach循环中填充PHP数组_Php_Arrays - Fatal编程技术网

在foreach循环中填充PHP数组

在foreach循环中填充PHP数组,php,arrays,Php,Arrays,我想将每个用户添加到一个数组中,并在添加之前检查重复的用户 $spotcount = 10; for ($topuser_count = 0; $topuser_count < $spotcount; $topuser_count++) //total spots { $spottop10 = $ids[$topuser_count]; $top_10 = $gowalla->getSpotInfo($spottop10); $usercount = 0; $

我想将每个用户添加到一个数组中,并在添加之前检查重复的用户

 $spotcount = 10;    


for ($topuser_count = 0; $topuser_count < $spotcount; $topuser_count++)     //total spots
{

$spottop10 = $ids[$topuser_count];
$top_10 = $gowalla->getSpotInfo($spottop10);
$usercount = 0;
$c = 0;
$array = array();

foreach($top_10['top_10'] as $top10)        //loop each spot
{
    //$getuser = substr($top10['url'],7);       //strip the url
    $getuser = ltrim($top10['url'], " users/" );

    if ($usercount < 3)     //loop only certain number of top users
     {  
        if (($getuser != $userurl) && (array_search($getuser, $array) !== true)) {

            //echo " no duplicates! <br /><br />";
            echo ' <a href= "http://gowalla.com'.$top10['url'].'"><img width="90" height="90"  src= " '.$top10['image_url'].' " title="'.$top10['first_name'].'" alt="Error" /></a>     ';                              
            $array[$c++] = $getuser;



        }
        else {

            //echo "duplicate <br /><br />";
        }

    }
    $usercount++;
}
print_r($array);    


}
这是错误的。它会检查重复项,但只检查每个foreach循环的内容,而不是整个数组。如果我将所有内容都存储到$array中,这是怎么回事?

array\u search()
返回您正在搜索的内容的键(如果它在数组中)。您正在进行严格的不等式比较
==
true
相对,因此如果array\u search确实在数组中找到一个条目(比如,键是7),那么
7!==TRUE
为TRUE,然后继续将条目添加到新数组中

您需要的是
array\u search(…)!==FALSE
,仅当数组搜索失败时才会计算为true


另外,不需要使用
$c++
数组索引计数器。您可以使用
$array[]=$getuser
,它将自动将$getuser粘贴到数组末尾的新条目中。

对多维数组使用下面的函数

function in_multiarray($elem, $array)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom] == $elem)
                return true;
            else
                if(is_array($array[$bottom]))
                    if(in_multiarray($elem, ($array[$bottom])))
                        return true;

            $bottom++;
        }       
        return false;
    }
_multiarray($elem,$array)中的函数 { $top=sizeof($array)-1; $bottom=0;
而($bottom使用标准PHP库(SPL)进行更快更干净的递归多维数组搜索

function in_multiarray($elem, $array)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom] == $elem)
                return true;
            else
                if(is_array($array[$bottom]))
                    if(in_multiarray($elem, ($array[$bottom])))
                        return true;

            $bottom++;
        }       
        return false;
    }
function in_array_recursive($needle, $haystack) {
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));

    foreach($it as $element) {
        if($element == $needle) {
            return true;
        }
    }

    return false;
}