[英]Compare multidimensional array values by key in php
我有一系列产品,如下面的示例所示。 产品可以是两个,也可以是三个甚至更多。 在这个例子中,三个。
$all_products = array(
'product_1' =>array(
'price' =>'$100',
'quantity' =>'2pcs.',
'availability'=>'In Stock',
'manufacturer'=>'Apple'),
'product_2' =>array(
'price' =>'$200',
'quantity' =>'2pcs.',
'availability'=>'In Stock',
'manufacturer'=>''),
'product_3' =>array(
'price' =>'$300',
'quantity' =>'2pcs.',
'availability'=>'In Stock',
'manufacturer'=>'')
);
我需要按每个键比较产品的值。 在比较表中突出显示价格、数量、可用性或制造商不同的行。
我试过这个 function 来检查哪些值不同并返回一个临时数组:
function compare_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (isset($val[$key])) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
}
$i++;
}
return $temp_array;
}
接着:
foreach ($all_products as $products) {
foreach ($products as $product_key => $val) {
foreach ($this->compare_array($all_products, $product_key) as $temp_value) {
if ($val != $temp_value) {
$style[$product_key] = 'style="background-color: lightblue;"';//style for highlight
}
}
}
}
问题是当数组中的某个值为空时。 就像在这个例子中, manufacturer
。
也许有人有更轻的解决方案?
我需要按每个键比较产品的值。 在比较表中突出显示价格、数量、可用性或制造商不同的行。
如果您想突出显示所有产品,除非它们的价格、数量、可用性或制造商完全相同。
function:
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;
}
仅当所有产品的字段具有完全相同的键和值时才返回true
,否则返回false
所以你这样使用它:
$identicalFlag = productsIdentical($all_products);
if ($identicalFlag === false) {
echo "Products are not identical:" . PHP_EOL;
$nonIdenticalProductsArr = array_keys($all_products);
echo "Non identical products are:" . PHP_EOL;
print_r($nonIdenticalProductsArr);
//do your styling on $nonIdenticalProducts
} else {
echo "Products are identical" . PHP_EOL;
}
Output:
对于相同的产品:
Products are identical
对于不相同的:
Products are not identical:
Non identical products are:
Array
(
[0] => product_1
[1] => product_2
[2] => product_3
)
或者,如果您想检测阵列中所有产品中不同的每个产品字段,请使用此 function:
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);
}
这边走:
$fieldsNonIdentical = getFieldsNonIdentical($all_products);
if (!empty($fieldsNonIdentical)) {
echo "Fields that are non identical:" . PHP_EOL;
print_r($fieldsNonIdentical);
//do your styling
$nonIdenticalStyle = 'style="background-color: lightblue;"';
$styleArr = [];
foreach ($fieldsNonIdentical as $key => $value) {
$styleArr[$value] = $nonIdenticalStyle;
}
echo "Non Identical fields styling is:" . PHP_EOL;
print_r($styleArr);
} else {
echo "All fields in all products are the same." . PHP_EOL;
}
Output
对于相同的:
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;"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.