PHP Parse $_POST Array?

后端 未结 3 1704
傲寒
傲寒 2021-01-14 11:48

A server sends me a $_POST request in the following format:

POST {
  array1
  {
    info1,
    info2,
    info3
  },
  info4
}

So naturally

相关标签:
3条回答
  • 2021-01-14 12:05

    try

    $_POST['array1'][0]
    $_POST['array1'][1]
    $_POST['array1'][2]
    
    0 讨论(0)
  • 2021-01-14 12:07

    You can simply use a foreach loop on the $_POST

    foreach($_POST["array1"] as $info)
    {
        echo $info;
    }
    

    or you can access them by their index:

    for($i = 0; $i<sizeof($_POST["array1"]); $i++)
    {
        echo $_POST["array1"][$i];
    }
    
    0 讨论(0)
  • 2021-01-14 12:24

    Use index notation:

    $_POST['array1'][0]
    $_POST['array1'][1]
    $_POST['array1'][2] 
    

    If you need to iterate over a variable response:

    for ($i = 0, $l = count($_POST['array1']); $i < $l; $i++) {
        doStuff($_POST['array1'][$i]);
    }
    

    This more or less takes this shape in plain PHP:

    $post = array();
    $post['info'] = '#';
    $post['array1'] = array('info1', 'info2', 'info3');
    

    http://codepad.org/1QZVOaw4

    So you can see it's really just an array in an array, with numeric indices.


    Note, if it's an associative array, you need to use foreach():

    foreach ($_POST['array1'] as $key => $val) {
        doStuff($key, $val);
    }
    

    http://codepad.org/WW7U5qmN

    0 讨论(0)
提交回复
热议问题