0

我有两个数组如下:

$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 的状态为启用,则不应调用该函数,并且下一个 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++;
      }
    }
  }
4

5 回答 5

2

您不需要嵌套的 foreach 来执行此操作。这很容易做到array_combine()。通过使用 ids 作为键和状态作为值来创建一个新数组,然后遍历数组并根据需要调用函数:

$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++;
    }
}

演示!

于 2013-10-12T12:14:56.223 回答
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); 
    }
  }
于 2013-10-12T12:13:49.477 回答
1

你很近。由于两个数组都包含偶数数量的值,因此您可以获得$keywhile 循环元素:

 $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 循环,而只需同时读取另一个数组

于 2013-10-12T12:13:50.553 回答
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++;
      }
  }
于 2013-10-12T12:15:12.673 回答
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);
}
于 2013-10-12T12:39:22.450 回答