1

I'm using POST Method and I want the PHP script to return the data in JSON format

//data 1:

<input type="text" value="1" name="id[]">
<input type="text" value="aa" name="name[]">
<input type="text" value="cc" name="stuff[]">

//data 2:

<input type="text" value="2" name="id[]">
<input type="text" value="dd" name="name[]">
<input type="text" value="ff" name="stuff[]">

i want result be like :

{id:1,name:"aa",stuff:"cc"},{id:2,name:"dd",stuff:"ff"}

I understand that if we use json_encode($_POST,true) i will have :

{"id":["1","2"],"name":["aa","dd"],"stuff":["cc","ff"]}

i can do that with js using get method not post

id[]=1&name[]=aa&stuff=cc&id[]=2&name[]=dd&stuff[]=ff

Check my solution https://jsfiddle.net/cqvny3th/

Or what if we generate url from the post method using http_build_query, result is :

id[]=1&id[]=2&name[]=aa&name[]=dd&stuff=cc&stuff[]=ff

But my solution works only with :

id[]=1&name[]=aa&stuff=cc&id[]=2&name[]=dd&stuff[]=ff

Regards

1
  • 1
    That first result isn't valid JSON. Commented Mar 16, 2018 at 19:31

3 Answers 3

2

Rename your inputs, if you can.

<input type="text" value="1" name="data1[id]">
<input type="text" value="aa" name="data1[name]">
<input type="text" value="cc" name="data1[stuff]">

<input type="text" value="2" name="data2[id]">
<input type="text" value="dd" name="data2[name]">
<input type="text" value="ff" name="data2[stuff]">

That will group the data properly. Use array_values before json_encode so you'll get an array of objects rather than an object.

echo json_encode(array_values($_GET));
Sign up to request clarification or add additional context in comments.

2 Comments

Literally never knew you could do that. Wow, every day's a school day.
I'm sure I must have learned this somewhere on Stack Overflow at some point. :)
1

Definitely less elegant than @Don't Panic's solution, but in case you want/need to keep your name attributes as they are, this will work:

//prep
$repeated_post_vars = ['id', 'name', 'stuff'];
$arr = [];

//find which column has the most values, just in case they're not all equal
$num_items = max(array_map(function($col) {
    return !empty($_POST[$col]) ? count($_POST[$col]) : 0;
}, $repeated_post_vars));

//iterate over value sets
for ($g=0; $g<$num_items; $g++) {
    foreach($repeated_post_vars as $col)
        $tmp[$col] = !empty($_POST[$col][$g]) ? $_POST[$col][$g] : null;
    $arr[] = $tmp;
}

So if $_POST on submit looks like:

[
    'id' => [1, 2],
    'name' => ['foo', 'bar'],
    'stuff' => [3]
];

The code produces:

[{"id":1,"name":"foo","stuff":3},{"id":2,"name":"bar","stuff":null}]

Comments

0

Could you do something like this?

$list = array();
for ($i=0; $i<count($_POST['id']); $i++) {
    $item = new stdClass();
    foreach ($_POST as $key => $values)
        $item->{$key} = $values[$i];
    $list[] = $item;
}

print json_encode( $list );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.