如何整合两个用于在以下情况下/ foreach循环? [英] How to integrate two for/foreach loops in following scenario?
问题描述
我有两个数组如下:
$ user_ids =阵列
(
[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 =阵列
(
[0] =>启用
[1] =>禁用
[2] =>启用
[3] =>禁用
[4] =>禁用
[5] =>禁用
[6] =>禁用
[7] =>启用
[8] =>禁用
[9] =>禁用
[10] =>禁用
[11] =>禁用
[12] =>禁用
[13] =>启用
[14] =>禁用
[15] =>禁用
[16] =>禁用
[17] =>启用
[18] =>禁用
[19] =>启用
)
第一个数组是给user_ids而其他阵列giveing每USER_ID的相应地位。使上述两个阵列将总是具有等于没有。的元素。
现在我想实现的是调用一个函数为每 user_ids 数组的元素(USER_ID)的。但在此之前,我已经检查的user_id这是会在函数调用中使用的状态是否应禁用。如果USER_ID的状态是使那么函数不应该被调用,下USER_ID应该付诸行动。我不能写出正确的逻辑这种情况。你能帮助我吗?提前致谢。 PHP的code,我试过是folows:
$ user_ids =爆炸('',$请求['项目']);
$ user_statuses =爆炸('',$请求['user_status']);
$计数= 0;
的foreach($ user_ids为$ USER_ID){
的foreach($ user_statuses为$ user_status){
如果(user_status =='禁用'){
$ objAdminUsers-> UpdateUserStatus($ USER_ID,$ user_status);
$计数++;
}
}
}
您不需要嵌套的foreach做到这一点。这很容易与 array_combine做()
。通过使用ID为键创建一个新的数组,通过数组状态的值,然后循环,并调用该函数,你需要它:
$阵列= array_combine($ user_ids,$ user_statuses);
$阵列= array_map('微调',$阵列); //删除空格
的foreach($数组的$ id => $状态){
如果($状态=='禁用'){
$ objAdminUsers-> UpdateUserStatus($ ID,$地位);
$计数++;
}
}
I'm having two arrays as follows:
$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
)
The first array is giving user_ids whereas the other array is giveing every user_id's corresponding status. SO that both the above arrays will always have equal no. of elements. Now what I want to achieve is to call a function for each of the element(user_id) of user_ids array. But before that I've to check whether the status of that user_id which is going to use in a function call should be disable. If the status of the user_id is enable then the function should not get called and the next user_id should come into action. I'm not able to write the correct logic for this situation. Can you help me in it? Thanks in advance. The PHP code which I tried is as folows:
$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++;
}
}
}
You don't need a nested foreach to do this. It's easy to do with array_combine()
. Create a new array by using ids as keys and statuses as values, and then loop through the array and call the function as you need it:
$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++;
}
}
这篇关于如何整合两个用于在以下情况下/ foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!