doutuanxiao4619
2015-08-25 08:42
浏览 136
已采纳

如何使用for循环比较两个数组?

I have 2 explode arrays from the database. and this is what i did.

    $searches = explode(',', $searchengine);
    $icons = explode(',', $icon);
    $b = count($searches);
    $c = count($icons);

I also made an array to compare each explode array to.

$searchesa = array("google","yahoo","bing");
    $d = count($searchesa);
    $iconsa = array("facebook","twitter","googleplus","linkedin","pinterest","delicious","stumbleupon","diigo");
    $y = count($iconsa);

Then i used for loops to travel to different array indexes. But the result is wrong, and sometimes I have an error which says UNDEFINED OFFSET.

for ($a=0; $a <$d ; $a++) {
    if ($searches[$a] == $searchesa[$a]) 
            {echo '<br>'.$searchesa[$a].': check ';
        }else
        echo '<br>'.$searchesa[$a].': chok ';
    }

for ($x=0; $x <$y ; $x++) {
    if ($icons[$x] == $iconsa[$x]) 
        echo '<br>'.$iconsa[$x].': check ';
    else
        echo '<br>'.$iconsa[$x].': chok ';
}

If the index from the database and the array I made are the same, it will state check, else it will state chok.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongshai1944 2015-08-25 08:56
    已采纳

    I posted this in my comment, but I suppose the outline will work better in an answer.

    I hope this could be of any help:

    <?php
    $array_a = ['test','test2']; // assume this is your first array
    $array_b = ['test']; // assume this is the array you wan to compare against
    $found = false;
    
    foreach ($array_a as $key_a => $val_a) {
        $found = false;
        foreach ($array_b as $key_b => $val_b) {
           if ($val_a == $val_b) {
                echo '<br>'. $val_b .': check ';     
                $found = true;
            }     
        }
        if (!$found)
            echo '<br>'. $val_a .': chok ';
    }
    ?>
    

    EDIT: Please excuse me for not testing it.

    This thing will loop through the first array, and compare it with every value in the other array.

    Tip: You can easily put this in a function and call it like compare($arr1, $arr2)

    打赏 评论
  • dongqiang1226 2015-08-25 08:44
    $arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs. 
    $arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
    

    taken via : PHP - Check if two arrays are equal

    打赏 评论
  • douxigai8757 2015-08-25 09:11

    You can try in_array method:

    $searchesa = array("google","yahoo","bing");
    $iconsa = array("facebook","twitter","googleplus","linkedin","pinterest","delicious","stumbleupon","diigo",'google');
    
    foreach($searchesa as $val){
        if(in_array($val, $iconsa)){
           echo "check";
        } else {
           echo "choke";
        }
    }
    

    Note: I've added "google" in $iconsa array.

    打赏 评论
  • dongyang7152 2015-08-25 09:15

    If I understand you correctly this is what you are looking for:

    // Lets prepare the arrays
    $searchEngines = explode(',', $searchengine);
    $icons = explode(',', $icon);
    
    // Now let's define the arrays to match with
    $searchEnginesCompare = array(
        'google',
        'yahoo',
        'bing'
    );
    
    $iconsCompare = array(
        'facebook',
        'twitter',
        'googleplus',
        'linkedin',
        'pinterest',
        'delicious',
        'stumbleupon',
        'diigo'
    );
    
    // Check the search engines
    foreach ($searchEngines as $k => $searchEngine) {
        if (in_array($searchEngine, $searchEnginesCompare)) {
            echo $searchEngine." : check<br />";
        } else {
            echo $searchEngine." : failed<br />";
        }
    }
    
    // Now let's check the icons array
    foreach ($icons as $k => $icon) {
        if (in_array($icon, $iconsCompare)) {
            echo $icon." : check<br />";
        } else {
            echo $icon." : failed<br />";
        }
    }
    
    打赏 评论