$POST variables after chopping a delimiter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $POST variables after chopping a delimiter

Post by AbraCadaver »

I'm not sure how you're posting your data, but you can use an array of names instead of using your own delimiters. The method will differ depending on whether you use cURL, fopen() etc. but here is an example with http_post_fields():

Code: Select all

$data = array('name'=>array('john'=>21,'peter'=>68,'steve'=>55));
 
http_post_fields('http://www.example.com/test.php', $data);
Now you can access $_POST['name']['john'] to get the age or if you want them as separate arrays:

Code: Select all

$name = array('john','peter','steve');
$age = array(21,68,55);
 
$data = compact('name', 'age');
 
http_post_fields('http://www.example.com/test.php', $data);
Now you can access $_POST['name'][0] and $_POST['age'][0] to get name 'john' and age '21'. Several ways to do it depending upon how you want to access the data in $_POST.

HTH
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $POST variables after chopping a delimiter

Post by AbraCadaver »

gromlok wrote:Thank you for the answer

I'm doing an script in Second Life -LSL is called the language-.
I can not use an array, or list. The only type that i'm allowed to use is string, that's why I send that big string with information with the delimiter, and got this problem!.
I'm not familiar with LSL, but construct the post like this:

Code: Select all

age%5B0%5D=21&age%5B1%5D=68&age%5B2%5D=55&name%5B0%5D=john&name%5B1%5D=peter&name%5B2%5D=steve
%5B is a [ and %5D is a ] with the array index in between %5B0%5D, %5B1%5D and so on. So in actuality it is an array like:

Code: Select all

age[0]=21&age[1]=68&age[2]=55&name[0]=john&name[1]=peter&name[2]=steve
There may be some URL/URI encode feature in LSL that will do this or just do it yourself. This is a properly URL encoded string that will be decoded as an array automatically when it hits the receiving page. You can then read the $_POST array in PHP.

Code: Select all

echo $_POST['name'][0];  // john
echo $_POST['age'][0];  // 21
Or in a simpler form as in the first example in my last post it would be:

Code: Select all

john=21&peter=68&steve=55
You can then loop through the $_POST:

Code: Select all

foreach($_POST as $name => $age) {
   echo "Name: $name";
   echo "Age: $age";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply