开发者

Group by the array value

I'm using this code:

$permissions = array("canview", "canpostthreads", "canpostreplies", "canpostpolls", "all");
foreach($permissions as $permission) {
  for ($i = 1; $i <= 5; $i++) {
    $mode = $_POST['permission'][$i][$permission];
    if($mode == 1) 
      echo "{$permission} = {$i}:::";
  }
}

And the output if I check some checkboxes is:

canview = 1:::canview = 5:::canpostreplies = 3:::canpostpolls = 5:::

I wan't the output to be following:

instead of canview = 1:开发者_运维百科::canview = 5:

canview = 1,5

and if I'll have for example:

canpostpolls = 1:::canpostpolls = 2:::canpostpolls = 3

It'll be:

canpostpolls = 1,2,3:::canview = 1,5

I hope you understand it guys. This is my own idea for this, you are free to share your ideas with me, this data will be exported to the mysql table.


$permissions = array("canview", "canpostthreads", "canpostreplies", "canpostpolls", "all");
$setpermissions = array();
foreach($permissions as $permission) {
  for ($i = 1; $i <= 5; $i++) {
    $mode = $_POST['permission'][$i][$permission];
    if($mode == 1) {
      if (!isset($setpermissions[$permission])) {
         $setpermissions[$permission] = array();
      }
      $setpermissions[$permission][] = $i;
    }
  }
}
$plist = array();
foreach ($setpermissions as $name => $sp) {
   $plist[] = "$name = " . implode(',', $sp);
}
echo implode(':::', $plist);


You need to filter your data a bit more... maybe something like this:

$permissions = array("canview", "canpostthreads", "canpostreplies", "canpostpolls", "all");
$filtered_perms = array();
foreach($permissions as $permission) {
  for ($i = 1; $i <= 5; $i++) {
    $mode = $_POST['permission'][$i][$permission];
    if($mode == 1) {
       if(!is_array($filtered_perms[$permission])) {
         $filtered_perms[$permission] = array();
       }
       $filtered_perms[$permission][] = $i;
  }
}

Then, you can do something like:

 $final_perms = array();
 foreach($filtered_perms as $key => $val) {
      $final_perms[$key] = implode(",", $val);
  }

Hope that helps!


$permissions = array("canview", "canpostthreads", "canpostreplies", "canpostpolls", "all");
$userPermissions = array();
foreach($permissions as $permission) {
  for ($i = 1; $i <= 5; $i++) {
    $mode = $_POST['permission'][$i][$permission];
    if($mode == 1) 
      $userPermissions[$permission][] = $i;
  }
}
foreach($userPermissions as $permission => $values) {
  echo "{$permission} = " . implode(',', $values) . ":::";
}


You can do something like this - essentially, combining the values before outputting them.

$permissions = array("canview", "canpostthreads", "canpostreplies", "canpostpolls", "all");
foreach($permissions as $permission) {
  $vals = array();
  for ($i = 1; $i <= 5; $i++) {
    $mode = $_POST['permission'][$i][$permission];
    if($mode == 1) 
      $vals[] = $i;
  }
  if(count($vals))
    echo $permission . ' == ' . implode(',', $vals);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜