按数组值分组

发布于 2024-12-06 13:24:50 字数 785 浏览 0 评论 0原文

我正在使用这段代码:

$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}:::";
  }
}

如果我选中一些复选框,输出是:

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

我不希望输出如下:

而不是 canview = 1:::canview = 5:

canview = 1,5

并且如果我'例如:

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

它将是:

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

我希望你们能理解。这是我自己的想法,你可以随意与我分享你的想法,这个数据将导出到mysql表中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是偏爱你 2024-12-13 13:24:50
$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);
$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);
只是偏爱你 2024-12-13 13:24:50

您需要更多地过滤数据...也许是这样的:

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

然后,您可以执行以下操作:

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

希望有帮助!

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!

2024-12-13 13:24:50
$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) . ":::";
}
$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) . ":::";
}
隐诗 2024-12-13 13:24:50

您可以执行类似的操作 - 本质上是在输出值之前将它们组合起来。

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文