Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中按键比较多维数组值_Php_Arrays_Multidimensional Array - Fatal编程技术网

在php中按键比较多维数组值

在php中按键比较多维数组值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一系列产品,如下面的示例所示。产品可以是两个或三个或更多。在这个例子中,三个 $all_products=array( 'product_1'=>数组( '价格'=>'100美元', “数量”=>“2件”, '可用性'=>'库存', “制造商”=>“苹果”), “product_2”=>数组( '价格'=>'200美元', “数量”=>“2件”, '可用性'=>'库存', “制造商”=>”, “product_3”=>数组( '价格'=>'300美元', “数量”=>“2件”, '可用性'=

我有一系列产品,如下面的示例所示。产品可以是两个或三个或更多。在这个例子中,三个

$all_products=array(
'product_1'=>数组(
'价格'=>'100美元',
“数量”=>“2件”,
'可用性'=>'库存',
“制造商”=>“苹果”),
“product_2”=>数组(
'价格'=>'200美元',
“数量”=>“2件”,
'可用性'=>'库存',
“制造商”=>”,
“product_3”=>数组(
'价格'=>'300美元',
“数量”=>“2件”,
'可用性'=>'库存',
'制造商'=>'')
);
我需要按每个键比较产品的价值。突出显示比较表中价格、数量、可用性或制造商不同的行

我尝试使用此函数检查哪些值不同,并返回一个临时数组:

函数比较\u数组($array,$key){
$temp_array=array();
$i=0;
$key_array=array();
foreach($val形式的数组){
如果(isset($val[$key])){
如果(!in_数组($val[$key],$key_数组)){
$key_数组[$i]=$val[$key];
$temp_数组[$i]=$val;
} 
}
$i++;
}
返回$temp_数组;
}   
然后:

foreach($products形式的所有产品){
foreach($product as$product\U key=>val){
foreach($this->compare_array($all_products,$product_key)作为$temp_value){
如果($val!=$temp\u值){
$style[$product_key]=“style=”背景色:浅蓝色;“;//突出显示的样式
}
}
} 
}
问题是当数组中的某个值为空时。如本例所示,
制造商

也许有人有更轻的解决方案

我需要按每个键比较产品的价值。突出显示行的步骤 在比较表中,价格、数量、可用性或 制造商不同

如果要突出显示所有产品,除非所有产品的价格、数量、可用性或制造商完全相同

功能:

function productsIdentical(array &$products) : bool
{
    if (count($products) < 2) {
        throw new \InvalidArgumentException("You should pass at least 2 products to compare");
    }

    $compare = '';
    foreach ($products as $product) {
        ksort($product); //to make comparison of key order insensitive
        $sha = sha1(json_encode($product));
        if (empty($compare)) {
            $compare = $sha;
        } elseif ($sha !== $compare) {
            return false;
        }
    }
    return true;
}
function getFieldsNonIdentical(array &$products) : array
{
    if (count($products) < 2) {
        throw new \InvalidArgumentException("You should pass at least 2 products to compare");
    }

    $compareArr = [];
    $keyDifferentArr = [];
    foreach ($products as $product) {
        foreach($product as $key => $val) {
            if (!key_exists($key, $compareArr)) {
                $compareArr[$key] = $val;
            } elseif ($compareArr[$key] !== $val) {
                $keyDifferentArr[$key] = true;
            }
        }
    }
    return array_keys($keyDifferentArr);
}
输出:

对于相同的产品:

Products are identical
对于不相同的:

Products are not identical:
Non identical products are:
Array
(
    [0] => product_1
    [1] => product_2
    [2] => product_3
)
All fields in all products are the same.
Fields that are non identical:
Array
(
    [0] => price
    [1] => manufacturer
)
Non Identical fields styling is:
Array
(
    [price] => style="background-color: lightblue;"
    [manufacturer] => style="background-color: lightblue;"
)
或者,如果要检测阵列中所有产品中不相同的每个产品字段,请使用此功能:

function getFieldsNonIdentical(array &$products) : array
{
    if (count($products) < 2) {
        throw new \InvalidArgumentException("You should pass at least 2 products to compare");
    }

    $compareArr = [];
    $keyDifferentArr = [];
    foreach ($products as $product) {
        foreach($product as $key => $val) {
            if (!key_exists($key, $compareArr)) {
                $compareArr[$key] = $val;
            } elseif ($compareArr[$key] !== $val) {
                $keyDifferentArr[$key] = true;
            }
        }
    }
    return array_keys($keyDifferentArr);
}
输出

对于相同的:

Products are not identical:
Non identical products are:
Array
(
    [0] => product_1
    [1] => product_2
    [2] => product_3
)
All fields in all products are the same.
Fields that are non identical:
Array
(
    [0] => price
    [1] => manufacturer
)
Non Identical fields styling is:
Array
(
    [price] => style="background-color: lightblue;"
    [manufacturer] => style="background-color: lightblue;"
)
对于不相同的:

Products are not identical:
Non identical products are:
Array
(
    [0] => product_1
    [1] => product_2
    [2] => product_3
)
All fields in all products are the same.
Fields that are non identical:
Array
(
    [0] => price
    [1] => manufacturer
)
Non Identical fields styling is:
Array
(
    [price] => style="background-color: lightblue;"
    [manufacturer] => style="background-color: lightblue;"
)

工作非常感谢。