adyoi
adyoi

Reputation: 117

PHP Loop If same key then merge value

Sorry my english is bad, and i want to asking here for duplicate questions with so many answer maybe not in my case, i have an array produced from multiple array $_POST like

<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: 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

and i have handle with this code :

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

and this my $arr2 result :

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"]

How to merge value on same key while looping, My desire result is :

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,
            )
        )
)

Thanks for advice !

Upvotes: 0

Views: 241

Answers (4)

mickmackusa
mickmackusa

Reputation: 47900

If you are going to lump all this checkbox data into a table column as json, then I'll recommend a more condensed array structure before json encoding the data.

parse_str() will convert you querystring-formatted data into a desirable associative array of associative arrays (each value being 1).

From there, overwrite the subarrays to move the keys to values, then generate your json array.

Now your data can be simply INSERTed into your database as a compact, portable string.

Code: (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']));

Output:

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"]}

Upvotes: 2

PersianMan
PersianMan

Reputation: 964

Short Answer:

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

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


?>

if use jquery read this link to send object, if use text query string convert it : parse_str($_POST["checkbox"], $array);

php default behavior is:

<?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">

result:

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"
    }
  }
}

for your desire:

<?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>";

method2:

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

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


?>

Upvotes: 0

Code Spirit
Code Spirit

Reputation: 5061

Instead of using key as a key you could use actual array keys.

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

Upvotes: 0

Andreas
Andreas

Reputation: 23958

To only answer your question, how to transform arr2.
You can loop the array and build a new array.
There is most likely a better way with using the post array but my lunch break is over, so I'll leave that to 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

Upvotes: 0

Related Questions