我有两个阵列。
第一个数组是$newarray = ('489289', '536516', '332833', '536516')
第二个数组是
$rockin = array(
'489289' => array('536516','value1'),
'332833' => array('536516'),
);
我想删除$ newarray的一些值。 假设我们从$ newarray
循环所以在上面的情况下,489289会被删除(来自$ newarray) AS 536516在$ rockin数组中的关联值为489289而在$ newarray中也存在536516
直到现在我已经尝试过这段代码
foreach ($newarray as $group_id) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if($group_id==$myfrickingleader)
{
unset($newarray[$group_id]);
}
}
}
}
答案 0 :(得分:1)
这是我理解你想做的事情:
$newarray = array('489289', '536516', '332833', '536516');
$rockin = array(
'489289' => array('536516','332833'),
'332833' => array('536516'),
);
foreach ($rockin as $array) {
foreach ($array as $value) {
if (in_array($value, $newarray)) {
$key = array_search($array, $rockin);
$newarray = array_diff($newarray, array($key));
}
}
}
答案 1 :(得分:0)
您使用$ group_id作为键,但它是一个值。您必须按键取消设置,如下所示:
foreach ($i = 0; $i < count($newarray); $i++) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if ($newarray[$i] == $myfrickingleader) {
unset($newarray[$i]);
}
}
}
}
答案 2 :(得分:0)
foreach ($newarray as $k => $v) {
if(is_array($rockin[$v])){
foreach ($rockin[$v] as $key => $value) {
if(in_array($value, $newarray)){
unset($newarray[$k]);
}
}
}
}