我有两个数组如下:
$user_ids = Array
(
[0] => 159a8a6f1c00c5c4d5d0daaab2aa4227
[1] => a39777761f7816031aec676c80c3a8ad
[2] => 348703d858f2f36f3fed0da29d04a905
[3] => 05ce989ed54dcd3ecca8af9e0e373c5c
[4] => 2fe40d28847aad4f23ebf74cd93b7235
[5] => 717dce4998cc4f2b926a7a731c2ee651
[6] => 5d3dbc660f5896f790ad15cc40b14209
[7] => 41e400183bb54c2dd25dcd2f062a7ca9
[8] => a8e0f96c0f68242df08c91d58e14d5aa
[9] => ddb29b7d35a24b5bad77bab752ec921b
[10] => eaf925c5e43f387d7d0eba7ee45856e3
[11] => 5291d6ee6c5b577514e832f077a496ca
[12] => 89f9e41b866c8e1c09e66e183a4b9265
[13] => 836d8a3e18888ee2b79b9636a8227c08
[14] => f9f3830700718df102c6c54bc004bdf9
[15] => 982e5d10baf398cb07a0ed388d8e1577
[16] => eb461f406d36a46db88f59f4c0b7b7f1
[17] => 9bfd556bd9ee849048690898a9d1ed3c
[18] => 1f7ec7180354a8741d4e86949e5922fe
[19] => 75cd2b01677ac49306caed58b26d4781
)
$user_status = Array
(
[0] => enable
[1] => disable
[2] => enable
[3] => disable
[4] => disable
[5] => disable
[6] => disable
[7] => enable
[8] => disable
[9] => disable
[10] => disable
[11] => disable
[12] => disable
[13] => enable
[14] => disable
[15] => disable
[16] => disable
[17] => enable
[18] => disable
[19] => enable
)
第一个数组给出了user_ids,而另一个数组给出了每个user_id的相应状态。因此,上述两个阵列总是具有相同的否。元素。 现在我想要实现的是为 user_ids 数组的每个元素(user_id)调用一个函数。但在此之前,我要检查是否应禁用将在函数调用中使用的user_id的状态。如果user_id的状态为enable,则不应调用该函数,并且下一个user_id应该起作用。我无法为这种情况编写正确的逻辑。你能帮助我吗?提前致谢。我试过的PHP代码如下:
$user_ids = explode(',', $request['items']);
$user_statuses = explode(',', $request['user_status']);
$count = 0;
foreach ($user_ids as $user_id) {
foreach ($user_statuses as $user_status) {
if(user_status=='disable') {
$objAdminUsers->UpdateUserStatus($user_id, $user_status);
$count++;
}
}
}
答案 0 :(得分:2)
您不需要嵌套的foreach来执行此操作。使用array_combine()
很容易。使用id作为键和状态作为值创建一个新数组,然后遍历数组并根据需要调用该函数:
$array = array_combine($user_ids, $user_statuses);
$array = array_map('trim', $array); // remove whitespaces
foreach ($array as $id => $status) {
if($status == 'disable') {
$objAdminUsers->UpdateUserStatus($id, $status);
$count++;
}
}
答案 1 :(得分:1)
也许是这样的?
$user_ids = explode(',', $request['items']);
$user_statuses = explode(',', $request['user_status']);
foreach ($user_statuses as $key => $user_status) {
if(user_status=='disable') {
$objAdminUsers->UpdateUserStatus($user_ids[$key], $user_status);
}
}
答案 2 :(得分:1)
你很亲密。由于两个数组都包含偶数量的值,因此您可以在循环元素的同时获取$key
:
$user_ids = explode(',', $request['items']);
$user_statuses = explode(',', $request['user_status']);
$count = 0;
foreach ($user_ids as $key => $user_id) {
// $key contains the number between the brackets
if($user_statuses[$key]=='disable') {
$objAdminUsers->UpdateUserStatus($user_id, $user_statuses[$key]);
$count++;
}
}
}
这样你不需要第二个foreach循环,但你只需要同时读取另一个数组
答案 3 :(得分:1)
你只需要一个foreach,就像这样:
$user_ids = explode(',', $request['items']);
$user_statuses = explode(',', $request['user_status']);
$count = 0;
foreach ($user_ids as $key => $user_id) {
if($user_status[$key]=='disable') {
$objAdminUsers->UpdateUserStatus($user_id, $user_status[$key]);
$count++;
}
}
答案 4 :(得分:1)
参考:http://phpfiddle.org/main/code/8ni-kjz
while ($match = current($user_status)) {
if ($match == 'disable') {
$id = key($user_status);
objAdminUsers->UpdateUserStatus($user_ids[$id], $user_status[$id]);
$count++;
}
next($user_status);
}