array in $_POST method?

+4 Marin Bešlo · August 15, 2016
Hello,

I hope someone can help me. I'm trying to send an array from android to my php script. But how can I access an array with $_POST method?

$user_name = $_POST["user_name"]; //this is just a variable and one string

But how can I pass an array through the $_POST method with few user names, for example look at this pseudo code:

for($i = 1; $i <= $_POST['array'].length; $i++){
$user_name[$i] = $_POST['array'][$i];
}
//so, If I send 10 usernames, I'll have 10 variables etc...


I hope you understand my question.... thanks :D

Post a Reply

Replies

Oldest  Newest  Rating
+2 Jonah Morrison · August 16, 2016
Hi there!

The post array is filled with post values. I don't believe it is possible to send an array. I believe it is key => value sort of thing.

Also your for loop is incorrect. There is no ".length" in PHP. Use the count() function.

- Jonah
+1 Sampan Verma · August 16, 2016
You will have to build the form (HTML) such that is supports sending array. I mean I've done it once with multiple file uploads. Also as Jonah Morrison said, there is no length.

I think this is what you want is :
${$_POST["username"]} = $_POST["username"];
+1 Sampan Verma · August 16, 2016
Also use foreach loop with arrays. They are better.
0 Marcos Paegle · September 28, 2016
You can send the data in JSON from android to PHP and use json_decode. http://php.net/manual/en/function.json-decode.php
0 Ahad Raheman · November 1, 2016
please try using json to send and receive data
0 Muhammad Tahir Shaikh · November 13, 2016
use foreach($arrayVar  as $newvar){
echo $newvar;
}

inside of for loop. and out side too.
0 Ahad Raheman · November 13, 2016
Try to use var_dump( $_POST) , you will get the idea how to parse
+1 Casper Hollemans · November 15, 2016
A Json string is way better in this case.
If you'd send only a bit date a normal post woul'd be okay.
In Json you're able to use array's so that'll make it a bit easier.
Your Json string should look like this:
'{
   "usernames" : {
       "username_1" : "value",
       "username_2" : "value",
       "username_3" : "value"
   }
}'

After you managed to send this Json string in a post your code should look like this:
$usernames = Json_decode($_POST['usernames'], true)[1]; 
// This function doesn't need a second param but if you set it to true it'll return a array
// The reffrence to the 2nd element in the array is bc the name of the array in the Json string is the 1th. Keep that in mind. --> [1]

Now you have a array full of you usernames.

Hope it helped.
Greets :D
0 Genny joe · November 18, 2016
You have to access array of elements like this,

HTML:

<form name="form1" method="post"">
<input type="text" name="user_name[]" />
<input type="text" name="user_name[]" />
<input type="text" name="user_name[]" />
<input type="text" name="user_name[]" />
<input type="text" name="user_name[]" />
<input name="submit" value="Post Data" type="submit"/>
</form>

PHP:

if(isset($_POST['submit'])) {

foreach($_POST['user_name'] as $usr) {
echo $usr . '<br>';
}
0 Vuk Spasojević · January 19, 2017
Would agree with Genny joe solution as best one.
  • 1

PHP

131,215 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators