0

Very similar situation in this answer: Compare multidimensional array values by key in php Working when all child keys are identical, but problem when some child arrays are not identical or empty. So I decide to ask another question. Sample of the possible array:

$all_products = array(
    'product_1'       =>array(
        'price'       =>'$100',
        'brand'       =>'Apple'
    ),
    'product_2'       =>array(
        'price'       =>'$200',
        'quantity'    =>'2pcs.',
        'available'   =>'In Stock'
    ),
    'product_3'       =>array(
    )
);

In this sample all child arrays are different.


|key name | product_1 | product_2 | product_3 |
-----------------------------------------------
| price   | $100      | $200      |           | row should be highlighted
----------------------------------------------- 
| quantity|           | 2pcs.     |           | row should be highlighted
----------------------------------------------- 
|available|           | In Stock  |           | row should be highlighted
----------------------------------------------- 
| brand   |   Apple   |           |           | row should be highlighted
-----------------------------------------------          

I need compare this products and highlight rows where data in row is different. If all data in row are the same no highlighting. I have tried to use the way in mentioned answer Compare multidimensional array values by key in php Is working fine when all keys in a child rows are identical, but in this situation does not work... So no success. I got stuck at all... If anybody can help I'll be very thankful.

8
  • Do you have "finish" param or you can have more that those 4 (price, quantity, available, brand) ? And you want to highlight rows where data in row is different => different when you compare with what ? If you have row1 = row2 and row 3 = row 4, but row1 != row3, what will you highlight ? Commented Nov 14, 2019 at 9:17
  • @MickaëlLeger this is definitely page like compare products and they can have infinite properties. Commented Nov 14, 2019 at 9:18
  • Can be more ( 5, or 10, or more) parameters like those 4 (price, quantity, available, brand) Commented Nov 14, 2019 at 9:26
  • I don’t get what you want here. Please give an example of when you would not want to highlight a row then - like when the price was $100 for all three products, or what? “but in this situation does not work...” - give us a proper problem description - “does not work” is not one. Commented Nov 14, 2019 at 9:38
  • in case when, for example, "available" will be "In Stock" for all products, this row not highlighted. The same with another rows. In child array rows ( values by key name) can be more, or less. Products also can be more, or just only two. Commented Nov 14, 2019 at 10:08

1 Answer 1

2

If I clearly understood you then this is your solution:

$ar_keys = [];
$keys_highl = [];
$keys_values = [];
foreach($all_products as $prod_name => $data){
    foreach($data as $key => $val){
        if (!in_array($key, $ar_keys) && !empty($val)) {
            array_push($ar_keys, $key); 
            $keys_highl[$key] = 0;
            $keys_values[$key] = [];
        } 
        $keys_values[$key][] = $val;
    }
} 

$max_length = count($all_products); 

foreach($keys_highl as $k => $high){
    $keys_highl[$k] = 
          count(array_unique($keys_values[$k])) == 1 
          && count($keys_values[$k]) == $max_length 
          ?  0 : 1;
}

 print_r($keys_highl); 
 return;   

Working Demo

EDITED: I have edited little bit your code. I add additional condition in case if the child array some keys are empty at all. Was:

if (!in_array($key, $ar_keys)) {

Changed to this:

if (!in_array($key, $ar_keys) && !empty($val)) {
Sign up to request clarification or add additional context in comments.

5 Comments

@ Aksen P I have tried your code but seems does not work in all combination. Please check this DEMO Link: sandbox.onlinephpfunctions.com/code/…
@K.B., sorry, $max_length = count($all_products); should be instead of $max_length = count($keys_values);
Nice Thank's a lot, working fine. sandbox.onlinephpfunctions.com/code/…
@K.B., as you wish, this edition prevents of being all cells empty which means ok, cause it's the same value in each one. null is a value also. Nevermind, as you wish
@ Aksen P Yes, because if in some key (which values we need to compare) all values empty, it's mean that all values equal, but with previous code I got that they are not equal and I edited your code little bit. Anyway it is very helpful and I'm thankful for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.