PHP循环如果相同的键则合并值

时间:2018-10-04 10:25:52

标签: php arrays

对不起,我的英语不好,我想在这里问这么多重复的问题,也许就我而言,我有一个由多个数组$ _POST生成的数组,例如

<input type="checkbox" class="styled" data-controller="company-group" data-action="index" name="checkbox[company-group][index]" value="1" checked=""> 
<input type="checkbox" class="styled" data-controller="company-group" data-action="create" name="checkbox[company-group][create]" value="1" checked=""> 
<input type="checkbox" class="styled" data-controller="company-group" data-action="update" name="checkbox[company-group][update]" value="1" checked=""> 
<input type="checkbox" class="styled" data-controller="company-group" data-action="delete" name="checkbox[company-group][delete]" value="1" checked=""> 

<input type="checkbox" class="styled" data-controller="company-industry" data-action="index" name="checkbox[company-industry][index]" value="1" checked=""> 
<input type="checkbox" class="styled" data-controller="company-industry" data-action="create" name="checkbox[company-industry][create]" value="1" checked=""> 
<input type="checkbox" class="styled" data-controller="company-industry" data-action="update" name="checkbox[company-industry][update]" value="1" checked=""> 
<input type="checkbox" class="styled" data-controller="company-industry" data-action="delete" name="checkbox[company-industry][delete]" value="1" checked=""> 

$ _ POST ['复选框']

checkbox: checkbox%5Bcompany-group%5D%5Bindex%5D=1&checkbox%5Bcompany-group%5D%5Bview%5D=1&checkbox%5Bcompany-group%5D%5Bcreate%5D=1&checkbox%5Bcompany-group%5D%5Bupdate%5D=1&checkbox%5Bcompany-group%5D%5Bdelete%5D=1&checkbox%5Bcompany-industry%5D%5Bindex%5D=1&checkbox%5Bcompany-industry%5D%5Bview%5D=1&checkbox%5Bcompany-industry%5D%5Bcreate%5D=1&checkbox%5Bcompany-industry%5D%5Bupdate%5D=1&checkbox%5Bcompany-industry%5D%5Bdelete%5D=1

我可以处理以下代码:

$asi = urldecode($_POST['checkbox']);
$asu = explode('&', $asi);
$arr = array();
$arr2 = array();

foreach ($asu as $key) 
{
    list ($ass, $iss) = explode('=', $key);
    preg_match_all('/\[(.*?)\]/', $ass, $sue);
    $arr2[] = array_merge_recursive($sue[1]);
}

这是我的$ arr2结果:

print_r()

Array
(
    [0] => Array
        (
            [0] => company-group
            [1] => index
        )

    [1] => Array
        (
            [0] => company-group
            [1] => view
        )

    [2] => Array
        (
            [0] => company-group
            [1] => create
        )

    [3] => Array
        (
            [0] => company-group
            [1] => update
        )

    [4] => Array
        (
            [0] => company-group
            [1] => delete
        )

    [5] => Array
        (
            [0] => company-industry
            [1] => index
        )

    [6] => Array
        (
            [0] => company-industry
            [1] => view
        )

    [7] => Array
        (
            [0] => company-industry
            [1] => create
        )

    [8] => Array
        (
            [0] => company-industry
            [1] => update
        )

    [9] => Array
        (
            [0] => company-industry
            [1] => delete
        )
)

var_export()

array (
  0 => 
  array (
    0 => 'company-group',
    1 => 'index',
  ),
  1 => 
  array (
    0 => 'company-group',
    1 => 'view',
  ),
  2 => 
  array (
    0 => 'company-group',
    1 => 'create',
  ),
  3 => 
  array (
    0 => 'company-group',
    1 => 'update',
  ),
  4 => 
  array (
    0 => 'company-group',
    1 => 'delete',
  ),
  5 => 
  array (
    0 => 'company-industry',
    1 => 'index',
  ),
  6 => 
  array (
    0 => 'company-industry',
    1 => 'view',
  ),
  7 => 
  array (
    0 => 'company-industry',
    1 => 'create',
  ),
  8 => 
  array (
    0 => 'company-industry',
    1 => 'update',
  ),
  9 => 
  array (
    0 => 'company-industry',
    1 => 'delete',
  ),
)

json_encode()

[["company-group","index"],["company-group","view"],["company-group","create"],["company-group","update"],["company-group","delete"],["company-industry","index"],["company-industry","view"],["company-industry","create"],["company-industry","update"],["company-industry","delete"]

如何在循环时合并同一键上的值,我的期望结果是:

Array
(
    [0] => Array
        (
            [key] => company-group
            [val] => Array (
                        [0] => index,
                        [1] => create,
                        [2] => update,
                        [3] => delete,
            )
        )

    [1] => Array
        (
            [key] => company-industry
            [val] => Array (
                        [0] => index,
                        [1] => create,
                        [2] => update,
                        [3] => delete,
            )
        )
)

感谢您的建议!

4 个答案:

答案 0 :(得分:2)

如果要将所有复选框数据作为json汇总到表列中,那么在json对数据进行编码之前,我会建议使用更精简的数组结构。

parse_str()会将查询字符串格式的数据转换为所需的关联数组(每个值为1)。

从那里,覆盖子数组以将键移至值,然后生成json数组。

现在,您可以将数据作为紧凑的可移植字符串简单地插入到数据库中。

代码:(Demo

$_POST['checkbox'] = 'checkbox%5Bcompany-group%5D%5Bindex%5D=1&checkbox%5Bcompany-group%5D%5Bview%5D=1&checkbox%5Bcompany-group%5D%5Bcreate%5D=1&checkbox%5Bcompany-group%5D%5Bupdate%5D=1&checkbox%5Bcompany-group%5D%5Bdelete%5D=1&checkbox%5Bcompany-industry%5D%5Bindex%5D=1&checkbox%5Bcompany-industry%5D%5Bview%5D=1&checkbox%5Bcompany-industry%5D%5Bcreate%5D=1&checkbox%5Bcompany-industry%5D%5Bupdate%5D=1&checkbox%5Bcompany-industry%5D%5Bdelete%5D=1';
parse_str($_POST['checkbox'], $output);

var_export($output['checkbox']);
echo "\n---\n";
echo json_encode(array_map('array_keys', $output['checkbox']));

输出:

array (
  'company-group' => 
  array (
    'index' => '1',
    'view' => '1',
    'create' => '1',
    'update' => '1',
    'delete' => '1',
  ),
  'company-industry' => 
  array (
    'index' => '1',
    'view' => '1',
    'create' => '1',
    'update' => '1',
    'delete' => '1',
  ),
)
---
{"company-group":["index","view","create","update","delete"],"company-industry":["index","view","create","update","delete"]}

答案 1 :(得分:1)

简短答案:

$result=[];
foreach ($_POST["checkbox"] as $i=>$checkbox){
    $result[]=["key"=>$i,"val"=>array_keys($checkbox)];
}

echo "<pre>";
var_export($result);
echo "</pre>";


?>

如果使用jquery读取this link发送对象,如果使用文本查询字符串将其转换为parse_str($_POST["checkbox"], $array);

php的默认行为是:

<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
?>
<form action="test.php" method="post">
    <input type="checkbox" class="styled" data-controller="company-group" data-action="index" name="checkbox[company-group][index]" value="1" checked="">
    <input type="checkbox" class="styled" data-controller="company-group" data-action="create" name="checkbox[company-group][create]" value="1" checked="">
    <input type="checkbox" class="styled" data-controller="company-group" data-action="update" name="checkbox[company-group][update]" value="1" checked="">
    <input type="checkbox" class="styled" data-controller="company-group" data-action="delete" name="checkbox[company-group][delete]" value="1" checked="">
    <br>
    <input type="checkbox" class="styled" data-controller="company-industry" data-action="index" name="checkbox[company-industry][index]" value="1" checked="">
    <input type="checkbox" class="styled" data-controller="company-industry" data-action="create" name="checkbox[company-industry][create]" value="1" checked="">
    <input type="checkbox" class="styled" data-controller="company-industry" data-action="update" name="checkbox[company-industry][update]" value="1" checked="">
    <input type="checkbox" class="styled" data-controller="company-industry" data-action="delete" name="checkbox[company-industry][delete]" value="1" checked="">
    <input type="submit">

结果:

array(1) {
  ["checkbox"]=>
  array(2) {
    ["company-a"]=>
    array(2) {
      ["index"]=>
      string(1) "1"
      ["delete"]=>
      string(1) "1"
    }
    ["company-b"]=>
    array(3) {
      ["create"]=>
      string(1) "1"
      ["update"]=>
      string(1) "1"
      ["delete"]=>
      string(1) "1"
    }
  }
}

满足您的需求

<?php

$result=[];
foreach ($_POST["checkbox"] as $i=>$checkbox){
    $temp=["key"=>$i];

    foreach ($checkbox as $val_key=>$val){
        $temp["val"][]=$val_key;
    }

    $result[]=$temp;
}

echo "<pre>";
var_export($result);
echo "</pre>";

方法2:

$result=[];
foreach ($_POST["checkbox"] as $i=>$checkbox){
    $result[]=["key"=>$i,"val"=>array_keys($checkbox)];
}

echo "<pre>";
var_export($result);
echo "</pre>";


?>

答案 2 :(得分:0)

仅回答您的问题,如何转换arr2。
您可以循环数组并构建新的数组。
使用post数组最有可能是一种更好的方法,但是我的午休时间结束了,所以我将其留给Mickmack。

Foreach($arr2 as $sub){
    $new[$sub[0]]["key"] = $sub[0];
    $new[$sub[0]]["val"][] = $sub[1];
}

Var_dump(array_values($new));

https://3v4l.org/HKM7S

答案 3 :(得分:0)

您可以使用实际的数组键来代替使用key作为键。

foreach ($asu as $key) 
{
    list ($ass, $iss) = explode('=', $key);
    preg_match_all('/\[(.*?)\]/', $ass, $sue);
    $arr2[$key][] = $sue[1];
}