组合和消除数组中的键值对

发布于 2024-12-11 02:36:26 字数 580 浏览 0 评论 0原文

一个关于数组的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趴在窗边数星星i 2024-12-18 02:36:26

试试这个:

$third = array_diff_key($second,array_flip($first));

try this:

$third = array_diff_key($second,array_flip($first));
枯寂 2024-12-18 02:36:26

可能有一个内置函数可以实现此目的,但如果没有,请尝试:

$third = array();
foreach(array_keys($second) as $item)
  if(!in_array($item, $first))
    $third[$item] = $second[$item];

请注意,这假设 $first 不会有在 中没有相应键的项目>$第二。为了解决这个问题,您可以有一个额外的循环(我不知道您将 $third 中的值设置为多少,也许是 null

foreach($first as $item)
  if(!in_array($item, array_keys($second)))
    $third[$item] = null;

There could be a built-in function for this, but if there isn't, try:

$third = array();
foreach(array_keys($second) as $item)
  if(!in_array($item, $first))
    $third[$item] = $second[$item];

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, maybe null:

foreach($first as $item)
  if(!in_array($item, array_keys($second)))
    $third[$item] = null;
淡笑忘祈一世凡恋 2024-12-18 02:36:26

这非常简单,而且效率极高:

$third = $second;

foreach($first as $value)
{
    if (isset($third[$value]))
    {
      unset($third[$value]);
    }
}

This is pretty simple to do and this way is extremely efficient:

$third = $second;

foreach($first as $value)
{
    if (isset($third[$value]))
    {
      unset($third[$value]);
    }
}
逆夏时光 2024-12-18 02:36:26

这是你问题的答案

$first= array
(
    "0" => "users",
    "1" => "posts",
    "2" => "comments"
);
$firstf=array_flip($first);
$second = array
(
    "users" => "the_users",
    "posts" => "the_posts",
    "comments" => "the_comments",
    "options" => "the_options"
);
$third=array_diff_key($second,$firstf);
print_r($third);

this is the answer to ur question

$first= array
(
    "0" => "users",
    "1" => "posts",
    "2" => "comments"
);
$firstf=array_flip($first);
$second = array
(
    "users" => "the_users",
    "posts" => "the_posts",
    "comments" => "the_comments",
    "options" => "the_options"
);
$third=array_diff_key($second,$firstf);
print_r($third);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文