Search in 2 arrays

80 Views Asked by At

I have problem with search in arrays , in 2 arrays , the first array have one structure fixed and the second send the vars from url and can have some values empty , for example

FIRST ARRAY :

The structure it's this :

id value , cat value , subcat value and country value

$ar_val="134567,dogs,food,EEUU";

The second var get from URL

$ar_url="134567,dogs,toys,EEUU";

As you can see in the second var , $ar_url i have one value no same to the first structure of $ar_val in this case toys

I want get compare the fixed structure of $ar_val and get if true or false if have same value in the same order from $ar_val

I try this :

$ar_val="134567,dogs,food,EEUU";
$ar_url="134567,dogs,toys,EEUU";


$exp_ar_val=explode(",",$ar_val);
$exp_ar_url=explode(",",$ar_url);


foreach ($exp_ar_val as $exp_ar_val2)
{
$ar_val_end[]=$exp_ar_val2;
}

foreach ($exp_ar_url as $exp_ar_url2)
{
$ar_val_end2[]=$exp_ar_url2;
}



$end_results=array_intersect($ar_val_end,$ar_val_end2);

With this I want know for example: If I search by id and for example for cat , get result positive in 2 arrays i have the same data , but if search for subcat i can´t get results because are differents in one have food and in other have toys , but with array_intersect no get this for compare

Thank´s , Regards

4

There are 4 best solutions below

0
On

I think he wants to check if a value exists in either array. try this...

<?
function inarray($value,$array=array()){

    if(in_array($value,$array)){
        return true;
    }else{
        return false;
    }
}

$ar_val=array("134567","dogs","food","EEUU");
$ar_url=array("134567","dogs","toys","EEUU");

$a = inarray("food",$ar_val);
$b = inarray("food",$ar_url);

if($a!=false and $b!=false) 
    echo "Found in both arrays";
elseif($a==true and $b==false)
    echo "Found in first array";    
elseif($a==false and $b==true)
    echo "Found in second array";   
else
    echo "Not Found in any array";  

?>
0
On

You can use array_diff for this, which is perfect for comparison purposes:

<?php
    $ar_val= array ("0" => "134567", "1" => "dogs", "2" => "food", "3" => "EEUU");
    $ar_url= array ("0" => "134567", "1" => "dogs", "2" => "EEUU", "3" => "food");

    $result = array_diff($ar_val, $ar_url);

    $num = 0;
    if ($result != NULL) {
            echo "unique array keys: TRUE" . "\n\n";
            print_r($result);
            echo "\n\n";
        } else {
            echo "unique array keys: FALSE" . "\n\n";
        }

        foreach($ar_val as $key) {
            if ($ar_val[$num] != $ar_url[$num]) {
                echo "intersecting array key different: TRUE" . "\n> ";
                print_r($ar_val[$num]);
                echo "\n\n";
                }
            $num++;
            }
?>
0
On

Try this:

$ar_val="134567,dogs,food,EEUU";
$ar_url="134567,dogs,toys,EEUU";

$arrayVal = explode(',', $ar_val);
$arrayUrl = explode(',', $ar_url);

$maxLength = max(sizeof($arrayVal), sizeof($arrayUrl));
$arrayIdsEqual = array();
$arrayIdsDifferent = array();

for ($i = 0; $i < $maxLength; $i++) {
    if (isset($arrayVal[$i]) && isset($arrayUrl[$i])) {
        if ($arrayVal[$i] == $arrayUrl[$i]) {
            $arrayIdsEqual[] = $i;
        }
        else {
            $arrayIdsDifferent[] = $i;
        }
    }
    else {
        //you arrive here if you have 2 arrays that don't have the same size / sme number of variables
    }
}

//assuming your 2 arrays ALWAYS have the same size, you can use the following logic
if (empty($arrayIdsDifferent)) {
    echo '2 arrays are the same';
}
else {
    echo "Differences: \n";
    foreach ($arrayIdsDifferent as $indexDifferent => $currentIdDifferent) {
        $output = 'difference ' . ($indexDifferent + 1) . ': ';
        $output .= 'val = ' . $arrayVal[$currentIdDifferent];
        $output .= '; ';
        $output .= 'url = ' . $arrayUrl[$currentIdDifferent];
        echo $output;
        echo "\n";
    }
}

You can see this working here: http://3v4l.org/pTQns

0
On

Just compare them with equal without any complicated operations.

$ar_val = "134567,dogs,food,EEUU";
$ar_url = "134567,dogs,toys,EEUU";

$exp_ar_val = explode(",", $ar_val);
$exp_ar_url = explode(",", $ar_url);
var_dump($exp_ar_val == $exp_ar_url); // false, elements does not match

$exp_ar_url = $exp_ar_val;
var_dump($exp_ar_val == $exp_ar_url); // true, all elements match

shuffle($exp_ar_url);
var_dump($exp_ar_val == $exp_ar_url); // false, different order