0

我在数组中搜索时遇到问题,在 2 个数组中,第一个数组有一个固定结构,第二个数组从 url 发送 var,并且可以有一些值为空的值,例如

第一个阵列:

它的结构是这样的:

id 值、cat 值、subcat 值和国家值

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

第二个 var 从 URL 获取

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

正如您在第二个 var 中看到的那样,在这种情况下,$ar_url i 的值与 $ar_val 的第一个结构不同

我想比较 $ar_val 的固定结构,如果从 $ar_val 以相同的顺序具有相同的值,则获取 true 或 false

我试试这个:

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

有了这个我想知道例如:如果我按 id 搜索,例如 cat ,在 2 个数组中得到肯定的结果,我有相同的数据,但是如果搜索 subcat 我不能得到结果,因为一个人有不同的食物和在其他有玩具,但与 array_intersect 没有得到这个比较

感谢和问候

4

4 回答 4

0

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
于 2015-03-05T11:15:10.343 回答
0

尝试这个:

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

你可以在这里看到这个工作:http: //3v4l.org/pTQns

于 2014-05-18T21:55:24.797 回答
0

我认为他想检查任一数组中是否存在值。尝试这个...

<?
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";  

?>
于 2014-05-18T21:33:36.087 回答
0

您可以使用array_diff它,这非常适合比较目的:

<?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++;
            }
?>
于 2014-05-18T21:41:14.327 回答