PHP Parse $_POST Array?
22,147
Solution 1
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');
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);
}
Solution 2
try
$_POST['array1'][0]
$_POST['array1'][1]
$_POST['array1'][2]
Solution 3
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];
}
Author by
Yinan Wang
Learning Ruby, Got HTML, CSS, PHP, Javascript, .NET, BackTrack pretty much down. Most Useful: BackTrack...
Updated on August 19, 2020Comments
-
Yinan Wang over 3 years
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
-
Yinan Wang over 11 yearssame as above...shows individual characters rather than the value.
-
Jared Farrish over 11 yearsThen what you posted wasn't accurate to what you're actually handling.
:)
-
Yinan Wang over 11 yearsWill extract work? It worked for info# and got me a serialized array1, but what about the contents of array1?
-
Yinan Wang over 11 yearsYeah 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...
-
Jared Farrish over 11 yearsYou 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. -
Bjørn Thomsen over 11 yearsWell 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.
-
Anant Dabhi over 11 yearsuser for loop like
($counter=0;$counter<count($_post['array1']);$counter++){ $_POST['array1'][$counter]; }
-
Jared Farrish over 11 yearsThat 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. -
Jared Farrish over 11 yearsWhat I imagine you did, looking at it now, is that you enumerated the keys. What you didn't realize, I suppose, that in PHP-land, that looks like a numeric list syntax. So, I can see where the issue arose from.
-
Jared Farrish over 11 yearsMostly, just show key and value, e.g.,
array1 { list1 = val, list2 = val, ...}
Don't leave it up to interpretation. -
Jared Farrish over 11 yearsNote, I added a simple associative array example.
-
Yinan Wang over 11 yearsOH WAIT! The array1 they gave me is actually kinda just a string! So do I need to reformat the string to an array?
-
Jared Farrish over 11 yearsThey're actually sending you a PHP serialized variable? Wow. Most web services these days use JSON, which is easier to consume.
unserialize()
should work on whatever that value is. Is that in thePOST
? -
Yinan Wang over 11 yearsWell array1(which is payload) isn't in an array format. It's just a string. (that's .... supposed to be an array). So I can't really unserialize it.
-
Jared Farrish over 11 yearsYou said you had a
$_POST
array already? What happened to that? Is that PHP serialized variable the only member returned? See the secondunserialize()
example at the PHP docs and you'll see what you have edited into your question is a PHP serialized array. -
Yinan Wang over 11 yearsRight. It came (supposedly) as an array.(payload) (btw all information is recorded in SQL). When the payload array is uploaded BEFORE serialization, instead of appearing as "Array", it appears as the serialized form.
-
Yinan Wang over 11 yearsYeah, so I ended up not serializing it. Still not an array. ALso, just tried extract on $_POST['payload']. Says it's not of type array.
-
Jared Farrish over 11 yearsAre you serializing a POST array (that's actually an array) before putting it into the table? Use JSON encoding instead (
json_encode()
). PHP serialization is PHP specific and, IMO, error prone. I'm not quite following your problem; you should probably ask that as another question. -
Jared Farrish over 11 yearsIt serializes: codepad.org/KT03GPda Only using JSON, which has more or less become a standard method of encoding data on web systems (as well as others). PHP serialization, I would stay away from.
-
Yinan Wang over 11 yearsI'm pretty sure that the array I posted above (in the edit) is is in JSON format. Maybe json_decode will do the trick?
-
Jared Farrish over 11 yearsPosted to your server, or saved in the database? What is literally in the question (at the end), is a PHP serialized variable. Look closely. And yes, I would abandon PHP serialization. When I try to unserialize what's in the question, it doesn't work.
-
Yinan Wang over 11 yearsThe one-liner in the EDITTED section in is JSON, no? So should't I be decoding?
-
Jared Farrish over 11 yearsIt's not JSON. Seriously. Compare: codepad.org/CQm4otEX Which looks like what you posted?
-
Yinan Wang over 11 yearsThe second one? If you look at the question, and look at the part UNDER THE THANKS!, you'll see what I mean.
-
Jared Farrish over 11 yearsI am. Look:
a:2: {s:7:"payload";s:59:"{"amount":25
This means array, two parts, with one part string of seven characters and string of 59 characters (which honestly, that last part seems wrong?). Thata:#
ands:#
is not JSON. -
Yinan Wang over 11 yearsYou're saying that the s:59 should actually be a:#?
-
Jared Farrish over 11 yearsI don't know; I have a strong disliking of PHP serialized strings. That's saying that the following is a string of 59 characters, which means that value is not being interpreted as an array. Compare it to my test compare: codepad.org/CQm4otEX And you'll see it's not
s:59
. -
Jared Farrish over 11 yearsYou should ask another question; I don't know. See this fiddle, too, which seems to be closer to what you have: codepad.org/dKObkWoz My few interactions with PHP serialized variables have been "unfulfilling", so I'd ask someone else. Manual though, seems like a bad idea (if you're talking working on the string itself).
-
Yinan Wang over 11 yearsYeah since the characters preceding the s:59 is constant, and the length of the text to delete is constant. Anyways, thanks for the help. The json_encode returned an array, for once!