gpt4 book ai didi

php - Array PHP,检查是否存在多个元素到另一个数组

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:37 25 4
gpt4 key购买 nike

我有一个问题:

enter image description here

我有两个数组,一个是静态的,一个可以由用户更新...我想检查静态数组中的每个 id 是否存在另一个数组的 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
)
)

现在,我使用这个脚本来回显如图所示的最终输出(结果是静态数组):

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,但我不知道如何,我有点困惑......我想检查静态数组存在于另一个数组中,如果存在,则执行:

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

非常感谢:)

最佳答案

如果我没理解错的话,您想为静态数组中的每个条目指示其 ID 是否存在于用户数组中。

您可以使用 array_column 生成用户数组中所有 ID 的数组。然后使用 in_array 检查每个静态 ID 是否存在于该数组中。如果找到,则将值设置为 1,如果未找到,则设置为 0。

为了示例,我生成了一个新的最终输出数组。但是您可以将“找到的”值添加到静态数组的每个条目。

<?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 中可用。对于旧版本,请参阅 Recommended userland implementation for PHP lower than 5.5

作为 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']; }

关于php - Array PHP,检查是否存在多个元素到另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com