我如何比较两个数组和列表差异PHP? [英] How can I compare two arrays and list differences in PHP?

查看:145
本文介绍了我如何比较两个数组和列表差异PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个形式做到以下几点:

I'm building a form to do the following:


  • 打印用户和权限表,从MySQL拉动。每一个用户有权限是选中复选框,而且每一个他们缺乏是一个非检查框。

  • 允许管理员检查并取消复选框以授予或删除权限。

  • 当表单提交,表明只有其权限将被改变的用户一个确认页面,突出具体的变化。

  • 当更改确认后,修改相应的数据库。

要做到这一点,我创建的用户权限的两个数组:根据什么数据库显示,按照什么样的形式展之一,和一个

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

如果用户没有在MySQL的权限,将显示为0。如果他们缺乏在表单提交权限,它根本不会存在。

If a user lacks a permission in MySQL, it will be shown as a 0. If they lack a permission in the form submission, it simply won't exist.

下面是阵列的两个简单的例子:

Here are two simple examples of the arrays:


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

表单提交阵列(从用户1吊销秘密,并把它给用户2)


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

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

问题

如何优雅地结合了这两个数组以做出改变阵列,使得:

Question

How can I elegantly combine these two arrays to make a "changes" array, such that:


  • 在这两个相同权限的用户省略

  • 剩余用户的所有权限 - 公共,私人和秘密 - 设置为0或1

  • 每个权限是0,如果它是从表单提交阵列失踪,1如果是表单提交阵列

例如,上述合并将使:


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

尝试到目前为止


  • 作为第一步,我一直在使用的形式数组array_merge()试图第二次上市,以为它会覆盖数据库阵列,他们是不同的。相反,它删除了不同的元素。

  • 我试图建立一个foreach()语句两个数组比较,但是它变得复杂,我认为必须有一个更简单的方法

  • 哟!新的答案把我的注意力回到这个老问题。我得到这个工作,但后来我取消这个疯狂的code - 它太复杂回去和使用。相反,我写了一个PHP后端脚本在同一时间改变一个许可,然后写一个AJAX前端在其发送的变化。更简单的code和变化是瞬时的用户。亮点效果使瞬间,对网页什么改变了反馈。

    Whew! A new answer drew my attention back to this old question. I got this working, but later I scrapped this crazy code - it was way too complicated to go back and work with. Instead, I wrote a PHP back end script to change one permission at a time, then wrote an AJAX front end to send changes over it. Much simpler code, and the changes are instantaneous for the user. A highlight effect gives instant, on-page feedback about what has changed.

    推荐答案

    POST数据只包含你应该授予的权限。因此,建立一个基线设置为0然后提交合并的所有权限。

    The post data only contains the permissions you should grant. So, set up a "baseline" with all the permissions set to 0. Then merge with the submission.

    $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']);
    

    现在更新使用合并阵列数据库。这样,您将设置正确的权限的所有元素。

    Now update the database using the merged arrays. This way you will set correct permissions for all elements.

    这篇关于我如何比较两个数组和列表差异PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆