Bir foreach döngüsü içinde bir PHP dizi doldurmamak

3 Cevap php

Ben bir diziye her kullanıcı eklemek ve ben yapmadan önce çiftleri kontrol etmek isteyen.

 $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);    


}

Önceki kod yazdırır:

Array ( [0] => 62151 [1] => 204501 [2] => 209368 ) Array ( [0] => 62151 [1] => 33116 [2] => 122485 ) Array ( [0] => 120728 [1] => 205247 [2] => 33116 ) Array ( [0] => 150883 [1] => 248551 [2] => 248558 ) Array ( [0] => 157580 [1] => 77490 [2] => 52046 )

Yanlış olduğunu. Bu çiftleri kontrol, ancak bunun yerine bütün dizinin her foreach döngüsü sadece içeriği yok. Ben $ diziye şeyi saklamak ediyorsam ne bu?

3 Cevap

Bu dizide ise array_search(), aradığınız ne olursa olsun anahtarı döndürür. Eğer sıkı bir eşitsizlik karşılaştırma !== true karşı yapıyorum, yani konum array_search dizide bir giriş bulmak YAPAR (demek, anahtar 7), daha sonra 7 !== TRUE doğrudur, ve size yeni diziye girişi eklemek için devam edin.

Ne istediğiniz array_search başarısız yalnızca true olarak değerlendirmek, hangi array_search(...) !== FALSE olduğunu.

Ayrıca, $c++ dizi indeksi sayacı kullanmak gerek yoktur. Sen $array[] = $getuser hangi otomatik sonunda dizi yeni bir giriş içine $ GetUser sopa kullanabilirsiniz.

Çok boyutlu dizi için fonksiyonu altında kullanmayın

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

Daha fazla bilgi için in_array() bakın

Daha hızlı ve daha temiz Recursive multidimensional array search, Standart PHP Kütüphanesi (SPL) kullanarak.

function in_array_recursive($needle, $haystack) {
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));

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

    return false;
}