PHP Parse $_POST Array?

arrays, parsing, php

Solution

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

Problem

A server sends me a $_POST request in the following format: ``` POST { array1 { info1, info2, info3 }, info4 } ``` So naturally, I could extract the info# very simply with `$_POST['#info']`. But how do I get the the three info's in the array1? I tried `$_POST['array1']['info1']` to no avail. Thanks! ``` a:2: {s:7:"payload";s:59:"{"amount":25,"adjusted_amount":17.0,"uid":"jiajia"}";s:9:"signature";s:40:"53764f33e087e418dbbc1c702499203243f759d4";} ``` is the serialized version of the POST

Original source