3

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

0

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

26 Comments

same as above...shows individual characters rather than the value.
Then what you posted wasn't accurate to what you're actually handling. :)
Yeah well can't do anything about that. Server that posts the data isn't mine. SIGHS This is what you get when you try to translate Ruby to PHP...
You don't understand me; see: codepad.org/aSLR61El Notice how similar that is to what you included as representing your actual POST array? That's the problem, what you demonstrated doesn't appear to be what you actually receive.
That doesn't change anything. You depicted what I have in the answer; whatever you have in reality, is not depicted in the question. :) Not a big deal, but it is hard to accurately answer a question with imperfect information.
|
2

try

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

1 Comment

user for loop like ($counter=0;$counter<count($_post['array1']);$counter++){ $_POST['array1'][$counter]; }
1

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

2 Comments

Will extract work? It worked for info# and got me a serialized array1, but what about the contents of array1?
Well you should try messing abit arround with it you can also just do an foreach($_POST as $post) and then loop down through that data you might wanna use <pre><%print_r($_POST);%></pre> while playing arround with it to see how your post is really formated.

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.