PHP数组:检查多个元素是否存在于另一个数组中

3

我对此有一个问题:

enter image description here

我有两个数组,一个是静态的,一个可以由用户更新...... 我想要检查每个ID是否存在于另一个数组中,并且如果存在则做一些事情,如果不存在(当完成检查时)则跳过到另一个ID等等...

现在,这些数组如下:

用户数组(用户解锁了2项成就):

Array (
  [0] => Array (
    [data] => Array (
      [importance] => 0
      [achievement] => Array (
        [id] => 644081262362202
        [title] => Achievement 2
        [type] => game.achievement
        [url] => http://www.***.com/achievements/achievement2.html
      )
    )
    [id] => 104693166566570
  )
  [1] => Array (
    [data] => Array (
      [importance] => 0
      [achievement] => Array (
        [id] => 968802826528055
        [title] => Achievement 1
        [type] => game.achievement
        [url] => http://www.***.com/achievements/achievement1.html
      )
    )
    [id] => 104023386633548
  )
) 

静态数组(有6个成就已保存):

Array (
  [0] => Array (
    [data] => Array (
      [points] => 50
    )
    [description] => you unlock the achievement2
    [title] => Achievement 2
    [id] => 644081262362202
  )
  [1] => Array (
    [data] => Array (
      [points] => 50
    )
    [description] => you unlock the achievement3
    [title] => Achievement 3
    [id] => 912599152147444
  )
  [2] => Array (
    [data] => Array (
      [points] => 50
    )
    [description] => you unlock the achievement5
    [title] => Achievement 5
    [id] => 913757345379232
  )
  [3] => Array (
    [data] => Array (
      [points] => 50
    )
    [description] => you unlock the achievement6
    [title] => Achievement 6
    [id] => 921989084564878
  )
  [4] => Array (
    [data] => Array (
      [points] => 50
    )
    [description] => you unlock the achievement1
    [title] => Achievement 1
    [id] => 968802826528055
  )
  [5] => Array (
    [data] => Array (
      [points] => 50
    )
    [description] => you unlock the achievement4
    [title] => Achievement 4
    [id] => 1149671038394021
  )
) 

现在,我使用这个脚本将最终输出像图片一样(results是静态数组):

if (empty($results)) {
  //echo 'noAchievement for the app';
} else {
  foreach ($results as $result) {
    $totalAchievementsApp .= ' [["' . "0" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";
  }
}

现在,我该怎么做才能检查这个脚本里面的内容呢?我知道我必须在else语句中添加另一个if来检查ID是否等于其他ID,但我不知道如何做,我有点困惑... 我想检查静态数组的ID是否存在于其他数组中,如果存在,就执行以下操作:

**$totalAchievementsApp .= ' [["' . "1" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";**

非常感谢您 : )

你可以按ID排序,然后循环两个数组,将其中一个的迭代器向前移动,直到它匹配/超过另一个。你可以遍历一个数组中的所有ID,并检查它们是否与第二个数组中的任何ID匹配(由于PHP数组是关联数组,因此在语言本身中实现这一点非常容易)... - CoffeDeveloper
1和0代表真和假吗,还是它代表匹配数量,因此2是可能性? - Ultimater
1个回答

1
如果我理解正确,您想要指示静态数组中的每个条目是否存在于用户数组中。
您可以使用array_column生成用户数组中所有ID的数组。然后使用in_array检查每个静态ID是否存在于该数组中。如果找到它,则将值设置为1,如果未找到,则将其设置为0。
为了举例说明,我已生成了一个新的最终输出数组。但是您可以将“found”值添加到静态数组的每个条目中。
<?php

$static=array(
    array('point'=>50,'title'=>'TITLE 1','id'=>54632),
    array('point'=>50,'title'=>'TITLE 2','id'=>54344),
    array('point'=>50,'title'=>'TITLE 3','id'=>34225),
    array('point'=>50,'title'=>'TITLE 4','id'=>2323245),
    array('point'=>50,'title'=>'TITLE 5','id'=>23872445),
);
$user=array(
    array('id'=>2323245,'title'=>'TITLE 1','point'=>50),
    array('id'=>54344,'title'=>'TITLE 2','point'=>50),
    array('id'=>34225,'title'=>'TITLE 3','point'=>50)
);

$final=array();

foreach ($static as $entry) {
    $final[]=array(
        'found'=>in_array($entry['id'],array_column($user,'id'))?1:0,
        'id'=>$entry['id'],
        'title'=>$entry['title'],
        'point'=>$entry['point']
    );
}

echo"<pre>".print_r($final,true)."</pre>";

使用您的数据,输出结果为:
Array
(
    [0] => Array
        (
            [found] => 0
            [id] => 54632
            [title] => TITLE 1
            [point] => 50
        )

    [1] => Array
        (
            [found] => 1
            [id] => 54344
            [title] => TITLE 2
            [point] => 50
        )

    [2] => Array
        (
            [found] => 1
            [id] => 34225
            [title] => TITLE 3
            [point] => 50
        )

    [3] => Array
        (
            [found] => 1
            [id] => 2323245
            [title] => TITLE 4
            [point] => 50
        )

    [4] => Array
        (
            [found] => 0
            [id] => 23872445
            [title] => TITLE 5
            [point] => 50
        )

)

编辑

考虑到您实际数组的更复杂结构,我嵌套了几个array_column函数来访问您的用户数组中更深层次的"data > achievement > id"键:

$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');

请看下面的例子:
// initialize the "static" and "user" arrays

$static=array (
  0 => array(
    'data' => array(
      'points' => 50
    ),
    'description' => 'you unlock the achievement2',
    'title' => 'Achievement 2',
    'id' => 644081262362202
  ),
  1 => array(
    'data' => array(
      'points' => 50
    ),
    'description' => 'you unlock the achievement3',
    'title' => 'Achievement 3',
    'id' => 912599152147444
  ),
  2 => array(
    'data' => array(
      'points' => 50
    ),
    'description' => 'you unlock the achievement5',
    'title' => 'Achievement 5',
    'id' => 913757345379232
  ),
  3 => array(
    'data' => array(
      'points' => 50
    ),
    'description' => 'you unlock the achievement6',
    'title' => 'Achievement 6',
    'id' => 921989084564878
  ),
  4 => array(
    'data' => array(
      'points' => 50
    ),
    'description' => 'you unlock the achievement1',
    'title' => 'Achievement 1',
    'id' => 968802826528055
  ),
  5 => array(
    'data' => array(
      'points' => 50
    ),
    'description' => 'you unlock the achievement4',
    'title' => 'Achievement 4',
    'id' => 1149671038394021
  )
);

$user=array(
  0=>array(
    'data' => array(
      'importance' => 0,
      'achievement' => array (
        'id' => 644081262362202,
        'title' => 'Achievement 2',
        'type' => 'game.achievement',
        'url' => 'http://www.***.com/achievements/achievement2.html'
      )
    ),
    'id' => 104693166566570
  ),
  1 => array (
    'data' => array (
      'importance' => 0,
      'achievement' => array (
        'id' => 968802826528055,
        'title' => 'Achievement 1',
        'type' => 'game.achievement',
        'url' => 'http://www.***.com/achievements/achievement1.html'
      )
    ),
    'id' => 104023386633548
  )
);

// build array of user achievement IDs
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');

// generate final array, with "found" values
$final=array();

foreach ($static as $entry) {
    $final[]=array(
        'found'=>in_array($entry['id'],$user_achvmts)?1:0,
        'id'=>$entry['id'],
        'title'=>$entry['title'],
        'description'=>$entry['description'],
        'points'=>$entry['data']['points']
    );
}

echo"<pre>".print_r($final,true)."</pre>";

结果是:

这里放结果

Array
(
    [0] => Array
        (
            [found] => 1
            [id] => 644081262362202
            [title] => Achievement 2
            [description] => you unlock the achievement2
            [points] => 50
        )

    [1] => Array
        (
            [found] => 0
            [id] => 912599152147444
            [title] => Achievement 3
            [description] => you unlock the achievement3
            [points] => 50
        )

    [2] => Array
        (
            [found] => 0
            [id] => 913757345379232
            [title] => Achievement 5
            [description] => you unlock the achievement5
            [points] => 50
        )

    [3] => Array
        (
            [found] => 0
            [id] => 921989084564878
            [title] => Achievement 6
            [description] => you unlock the achievement6
            [points] => 50
        )

    [4] => Array
        (
            [found] => 1
            [id] => 968802826528055
            [title] => Achievement 1
            [description] => you unlock the achievement1
            [points] => 50
        )

    [5] => Array
        (
            [found] => 0
            [id] => 1149671038394021
            [title] => Achievement 4
            [description] => you unlock the achievement4
            [points] => 50
        )

)

请注意,array_column仅在PHP >= 5.5.0中可用。对于旧版本,请参见低于5.5的PHP推荐用户实现
作为array_column的替代方案,您可以使用array_map来构建用户ID数组:
$user_achvmts = array_map( function($v) {return $v['data']['achievement']['id'];}, $user);

或者只需遍历用户数组:

$user_achvmts=[];
foreach ($user as $v) { $user_achvmts[]=$v['data']['achievement']['id']; }

非常感谢您的回复,我差不多解决了我的问题。字符串:'found'=>in_array($entry['id'],array_column($userAch,'id'))?1:0,找不到任何ID,所有的“found”都是0。我像这样访问用户的ID:$result[data][achievement][id],但如果我复制它,它就无法工作...非常感谢。 - Ribi
抱歉,我忽略了你的数组结构。请查看我的编辑。 - showdev
非常感谢,我用了另一种方法来获取作品,但是你的方法更好,代码更少,而且完美运行!非常感谢 :) - Ribi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接