Page 1 of 1

Re: $POST variables after chopping a delimiter

Posted: Tue Feb 16, 2010 12:11 pm
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

Re: $POST variables after chopping a delimiter

Posted: Tue Feb 16, 2010 9:36 pm
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";
}