1

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.

Peter
  • 8,776
  • 6
  • 62
  • 95
Aidyl Baylon
  • 53
  • 1
  • 11
  • Undefined offset? Then probably both arrays aren't of the same length. c: – Xyv Aug 25 '15 at 08:43
  • Aren't you looking for something like: `foreach ($array_a as $index_a => $value_a) foreach( $array_b as $index_b => $value_b) if ($value_a === $value_b) ...`? – Xyv Aug 25 '15 at 08:45
  • yeah it doesnt really have the same length. actually the explode array from the database is the values from a checkbox. i'm now trying to check on to what checkboxes are chosen because the output will be checkboxes that have check buttons to the ones that were chosen and cross button to the ones not chosen @Xyv – Aidyl Baylon Aug 25 '15 at 08:49
  • 1
    Wouldn't it be better to use a combination of `foreach`, `in_array` and `array_key_exists`? – Peter Aug 25 '15 at 08:50
  • can you show me how please? i really have no idea @Peter – Aidyl Baylon Aug 25 '15 at 08:52
  • @AidylBaylon I posted an answer to show you (`array_key_exists` wasn't required) – Peter Aug 25 '15 at 09:17

4 Answers4

2
$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

Community
  • 1
  • 1
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • It is a good suggestion, but wasn't op meaning to compare each value seperately? – Xyv Aug 25 '15 at 09:01
  • @Xyv it does the same thing.. why to compare via iteration? I agree, comparison is useful if we have to perform some sort of business logic based on match. – Danyal Sandeelo Aug 25 '15 at 09:03
  • Oh, I am not trying to criticise, its a board to help eachother out after all. however... I am not sure what their reason is, but it was in their question after all. so who knows what they want to do with the values on later base. – Xyv Aug 25 '15 at 09:06
1

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)

Xyv
  • 739
  • 7
  • 15
  • You are welcome ^^ I saw [Touqeer Shafi](http://stackoverflow.com/a/32199977/5173309) posted a good answer aswell, maybe you could combine these two suggestions. or just stick with one :p – Xyv Aug 25 '15 at 09:15
1

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.

Touqeer Shafi
  • 5,084
  • 3
  • 28
  • 45
0

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 />";
    }
}
Peter
  • 8,776
  • 6
  • 62
  • 95