How to compare two arrays and a list of differences in PHP?

I create a form to do the following:

  • Print a table of users and permissions, pulling from MySQL. Each permission of the user has a flag, and each of them lacks a flag.
  • Allow the administrator to check and uncheck the boxes to grant or remove permissions.
  • When the form is submitted, show the confirmation page with ONLY users whose permissions will be changed, highlighting specific changes.
  • When the changes are confirmed, modify the database accordingly.

To do this, I create two arrays of user permissions: one according to what the database shows, and one according to what the form shows.

If the user does not have permission in MySQL, it will be displayed as 0. If they do not have permission in the form submission, it simply will not exist.

Here are two simple examples of arrays:

Database array

[User1] => Array ([public] => 1
                [private] => 1
                [secret] => 1
               ) 
[User2] => Array ([public] => 1
                [private] => 0
                [secret] => 0
               )

Array of form submission (canceling the "secret" from User1 and providing it to user2)

[User1] => Array ([public] => 1
                [private] => 1 
               )

[User2] => Array ([public] => 1
                  [secret] => 1
                 )

Question

How can I elegantly combine these two arrays to create an array of "changes", for example:

  • Users with the same permissions are omitted in both.
  • - , - 0, 1.
  • 0, , 1,

, :

[User1] => Array ([public] => 1
                [private] => 1
                [secret] => 0
               ) 
[User2] => Array ([public] => 1
                [private] => 0
                [secret] => 1
               )

  • array_merge() , , , , . , .
  • foreach() , , ,

UPDATE

! . , - , . back back script , AJAX- . , . , .

+3
6
+2

, , PHP: . , , .

, array_diff() array_intersect(), .

+2

, . , " " , 0. .

$empty_perms = array("public" => 0, "private" => 0, "secret" => 0);

$new_user1_perms = array_merge($empty_perms, $_POST['User1']);
$new_user2_perms = array_merge($empty_perms, $_POST['User2']);

, . .

+2

gnud . "" .

// empty permissions
$empty_perms = array("public" => 0, "private" => 0, "secret" => 0, "member" => 0);

// original permissions (please note that it MUST contain ALL keys)
$old_perms = array("public" => 1, "private" => 0, "secret" => 1, "member" => 0);

// POST data container
$post_data = array("public" => 1, "private" => 1);

// apply new user permissions - put them into db
$new_perms = array_merge($empty_perms, $post_data);

// differences to show
$diff_perms = array();

// compare new and old permissions
foreach($empty_perms as $k => $v) {
    $diff_perms[$k] = (int)($old_perms[$k] !== $new_perms[$k]);
}

This example should give you the following results (old, new, changes):

Array
(
    [public] => 1
    [private] => 0
    [secret] => 1
    [member] => 0
)

Array
(
    [public] => 1
    [private] => 1
    [secret] => 0
    [member] => 0
)

Array
(
    [public] => 0
    [private] => 1
    [secret] => 1
    [member] => 0
)

Hope this helps.

+1
source

I wonder if any change in differential performance will help .

0
source

To use array_diff(), the first argument must be a superset or the larger of the two arrays. Use array_diff()when you are not sure which array is larger:

  //start with the superset of for array_diff to return differences
  if(count($prev_wholesaler_assignments) > count($wholesalers)){
      $perm_diff = array_diff($prev_wholesaler_assignments,$wholesalers);  
  } elseif (count($prev_wholesaler_assignments) < count($wholesalers)){
      $perm_diff = array_diff($wholesalers,$prev_wholesaler_assignments);
  }

This returns a clause, no matter which array is larger.

0
source

Source: https://habr.com/ru/post/1702598/


All Articles