0

In php, if you name your form fields using numerical indexes, they work as arrays in the $_POST object.

<form method="post" action="post.php">
    <input type="text" name="question[0][name]" />
    <input type="text" name="question[0][email]"/>
    <input type="text" name="question[0][password]" />
    <hr>
    <input type="text" name="question[1][name]" />
    <input type="text" name="question[1][email]"/>
    <input type="text" name="question[1][password]" />
    <hr>
    <input type="submit" value="Add" />
    <hr>
    <p><?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo json_encode($_POST, JSON_NUMERIC_CHECK);
}

     ?></p>
</form>

outputs

{"question":[{"name":"a","email":"aa","password":"aaa"},{"name":"b","email":"bb","password":"bbb"}]}

If the ordering of the fields is not sequential starting at zero and incrementing by only one each time the name is repeated, then they are all interpreted as keys instead. So

<form method="post" action="post.php">
    <input type="text" name="question[1][name]" />
    <input type="text" name="question[1][email]"/>
    <input type="text" name="question[1][password]" />
    <hr>
    <input type="text" name="question[0][name]" />
    <input type="text" name="question[0][email]"/>
    <input type="text" name="question[0][password]" />
    <hr>
    <input type="submit" value="Add" />
    <hr>
    <p><?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo json_encode($_POST, JSON_NUMERIC_CHECK);
}

     ?></p>
</form>

outputs

{"question":{"1":{"name":"a","email":"aa","password":"aaa"},"0":{"name":"b","email":"bb","password":"bbb"}}}

Is there a way to get $_POST to ignore the order of arrays of post keys so they are interpreted as an array?

2
  • Another way is to sort $_POST array when get. Commented Apr 29, 2016 at 5:53
  • @ShivaniPatel your example of this? Commented May 2, 2016 at 0:21

1 Answer 1

1

Please check if helpful or not :

<form method="post" action="#">
    <input type="text" name="question[1][name]" />
    <input type="text" name="question[1][email]"/>
    <input type="text" name="question[1][password]" />
    <hr>
    <input type="text" name="question[0][name]" />
    <input type="text" name="question[0][email]"/>
    <input type="text" name="question[0][password]" />
    <hr>
    <input type="submit" value="Add" />
    <hr>
    <p>
 <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {                     
            ksort($_POST['question']);          
            print_r($_POST['question']);
    }

 ?>
</p>
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

It will sort array according to keys.
In my first code, it doesn't matter what the field names are. In the "fix", the field names have to be known ahead of time. Is there a way to sort all field names in the POST object without necessarily knowing their names ?
Please check this helpful or not, i have implement function like this :i.imgur.com/8yfZN0f.png and output is i.imgur.com/7SiRiaR.png

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.