组合和消除数组中的键值对
一个关于数组的 PHP 问题。假设我们有两个数组:
[first] => Array
(
[0] => users
[1] => posts
[2] => comments
)
[second] => Array
(
[users] => the_users
[posts] => the_posts
[comments] => the_comments
[options] => the_options
)
如何比较这两个数组?意思是,在以某种方式组合(array_merge?)之后,我们如何检查第一个数组中的值是否等于第二个数组(array_flip?)中的键。无论哪个值/键对匹配,都将它们从数组中删除。
基本上,最终结果将是两个数组合并,删除重复项,唯一的索引是:
[third] => Array
(
[options] => the_options
)
A PHP question about arrays. Suppose we have two arrays:
[first] => Array
(
[0] => users
[1] => posts
[2] => comments
)
[second] => Array
(
[users] => the_users
[posts] => the_posts
[comments] => the_comments
[options] => the_options
)
How can I compare these two arrays? Meaning, how can we check whether or not the value in the first array is equal to the key in the second array (array_flip?) after combining them somehow (array_merge?). And whichever value/key pair matches, remove them from the array.
Basically, the end result would be the two arrays combined, duplicates removed, and the only index will be:
[third] => Array
(
[options] => the_options
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
try this:
可能有一个内置函数可以实现此目的,但如果没有,请尝试:
请注意,这假设
$first
不会有在中没有相应键的项目>$第二
。为了解决这个问题,您可以有一个额外的循环(我不知道您将$third
中的值设置为多少,也许是null
:There could be a built-in function for this, but if there isn't, try:
Note that this assumes that
$first
will not have an item that doesn't have a corresponding key in$second
. To account for this, you could have an additional loop (I don't know what you would set the value in$third
to for these, maybenull
:这非常简单,而且效率极高:
This is pretty simple to do and this way is extremely efficient:
这是你问题的答案
this is the answer to ur question