I have an array of the products as in the sample below. Products can be two, or three and more. In this example, three.
$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'=>'')
);
I need to compare values of the products by each key. To highlight rows in the compare table where price, quantity, availability, or manufacturer is different.
I have tried this function to check which values are different and return one temp array:
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;
}
and then:
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
}
}
}
}
Problem is when some value in array is empty. Like in this example, manufacturer.
Maybe someone has more light solution?