比较两个数组并获得差异

时间:2015-03-24 07:27:40

标签: php arrays

我有两个阵列。 $arr1是这样的: -

Array
(
    [Size] => L
    [Color] => Purple
    [Brand] => Lee
    [Fabric] => Cotton
)

和另一个数组$arr2是这样的: -

Array
(
    [0] => Array
        (
            [Color] => Purple
            [Size] => L
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 1000
            [Quantity] => 5
        )

    [1] => Array
        (
            [Color] => Pink
            [Size] => L
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 1100
            [Quantity] => 5
        )

    [2] => Array
        (
            [Color] => White
            [Size] => L
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 1200
            [Quantity] => 5
        )

    [3] => Array
        (
            [Color] => Black
            [Size] => L
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 900
            [Quantity] => 5
        )

    [4] => Array
        (
            [Color] => Purple
            [Size] => M
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 900
            [Quantity] => 5
        )

    [5] => Array
        (
            [Color] => Purple
            [Size] => S
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 900
            [Quantity] => 5
        )

    [6] => Array
        (
            [Color] => Pink
            [Size] => M
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 900
            [Quantity] => 5
        )

    [7] => Array
        (
            [Color] => Pink
            [Size] => S
            [Brand] => Lee
            [Fabric] => Cotton
            [Price] => 900
            [Quantity] => 5
        )
)

如果两个数组匹配,我想得到$arr2的价格和数量。例如,在这种情况下,$arr1具有与$arr2's第0个索引值相同的4个参数。因此,结果数组应返回1000和5.我已尝试array_diff和其他一些功能,但它并没有帮助我。任何帮助将不胜感激。提前谢谢。

4 个答案:

答案 0 :(得分:1)

    foreach($arr2 as $arrayIndex=>$element){
        $match = true;
        foreach($arr1 as $key=>$elementToMatch){
                if($element[$key] != $elementToMatch ){
                    $match = false;
                }
                if($match == false) {
                    break;
                }
            }
        if($match) {
            return $arr2[$arrayIndex];
        }
    }

我会建议这样的事情。这有点难看,因为循环中有循环,但这是实现这一目标的最简单方法。

答案 1 :(得分:0)

您 数组1: -

$Arr1=array(array
    (
        "Color" => "Pink",
        "Size" => "L",
        "Brand" => "Lee",
        "Fabric" => "Cotton",
        "Price" => "1100",
        "Quantity" => "5"
    ),array(
        "Color" => "White",
        "Size" => "L",
        "Brand" => "Lee",
        "Fabric" => "Cotton",
        "Price" => "1200",
        "Quantity" => "5"
    ),
    array(
        "Color" => "Black",
        "Size" => "L",
        "Brand" => "Lee",
        "Fabric" => "Cotton",
        "Price" => "1300",
        "Quantity" => "5"
    ),
     array(
        "Color" => "RED",
        "Size" => "L",
        "Brand" => "Lee",
        "Fabric" => "Cotton",
        "Price" => "1100",
        "Quantity" => "5"
    )

    );

数组2: -

$Arr2=array
(
    "Size" => "L",
    "Color" => "Purple",
    "Brand" => "Lee",
    "Fabric" => "Cotton"
);




for($x=0;$x<sizeof($Arr1);$x++){
        for($j=$x+1;$j<sizeof($Arr1);$j++){
            if($Arr1[$x]["Price"]==$Arr1[$j]["Price"] && $Arr1[$x]["Quantity"]==$Arr1[$j]["Quantity"]){
            $Arr2["Price"]=$Arr1[$j]["Price"];
            $Arr2["Quantity"]=$Arr1[$j]["Quantity"];
        } 
        }

}

    var_dump($Arr2);

您的输出将是

'Size' => string 'L' (length=1)
  'Color' => string 'Purple' (length=6)
  'Brand' => string 'Lee' (length=3)
  'Fabric' => string 'Cotton' (length=6)
  'Price' => string '1100' (length=4)
  'Quantity' => string '5' (length=1)

答案 2 :(得分:0)

假设价格和数量始终是最后一个,那么一个简单的方法就是这个

foreach($arr2 as $arr) {
    if(count(array_diff(array_slice($arr,0,4),$arr1))==0) {
        return $arr;
    }
}

答案 3 :(得分:0)

假设$arr1是一维且$arr2是二维的,您可以使用以下结果,其中包含匹配数组的pricequantity数组。

 function match_array($arr1, $arr2, $result){
  foreach($arr2 as $k=>$v){
    // calculate diff between array1 and array2[key]
    // if size of diff is 0 array matches
    if(sizeof(array_diff($arr1, $v)) == 0){
        // calculate the diff of array2[key] and array1
        // this will give us the price and quantity
        // push the diff into the result array
        array_push($result, array_diff($v, $arr1));
    }
  }
  print_r($result);
}
$result = array();
$result = match_array($array1, $array2, $result);
print_r($result);