如何关联下面给出的foreach循环?

时间:2012-07-05 12:29:26

标签: php arrays foreach

我在下面给出了两个for循环

foreach($result_access as $acc){
   $usr_access_id[] = $acc->id;
   $usraccess[] = $acc->rules;
}

>修订版

     foreach($somearray as $someid){//Updated
        foreach($usraccess as $accessusr){
          if(in_array($someid,$usraccess)){
            $myid = ??;///Here i want the $usraccess associated $acc->id, how can I get that? 
          }
        }
      }

正如您所看到的,我希望$myid获得$acc->id分配,该$usraccess应与当前{{1}}数组相关联

2 个答案:

答案 0 :(得分:2)

这是一个理智的解决方案:

foreach ($result_access as $acc) {
    if (in_array($someid, $acc->rules)) {
        $myid = $acc->id;
    }
}

疯狂的解决方案将是:

...

foreach($usraccess as $i => $accessusr){
  if(in_array($someid,$usraccess)){
    $myid = $usr_access_id[$i];
  }
}

答案 1 :(得分:0)

这是你想要的吗?

foreach($result_access as $acc){
   $usr_access_id[] = $acc->id;
   $usraccess[ $acc->id ] = $acc->rules;
}

foreach($usraccess as $accessusr){
  if(in_array($someid,$usraccess)){
    $myid = $usraccess[ $someid ] ;///Here i want the $usraccess associated $acc->id, how can I get that? 
  }
}