Page 1 of 1
Output Array Indexes
Posted: Thu Jul 08, 2010 8:38 am
by spacebiscuit
Hi,
I've come acorss this proble but cannot remembe the solution. I am trying to print the indexes of an array. When I do so I get one letter in each index which spells 'Array'.
I am trying the following:
echo"$_SESSION[0]";
echo"$_SESSION['del'][0]";
I'm not sure what I'm doing wrong - any ideas?
Thanks,
Rob.
Re: Output Array Indexes
Posted: Thu Jul 08, 2010 9:53 am
by spacebiscuit
I forgot to add that the $_SESSION variable is assigned from a $_GET variable, ie.
$_SESSION=$_GET[temp];
I'm not sure but is url_encode required, it rings a bell but I can't say for sure!
Rob.
Re: Output Array Indexes
Posted: Thu Jul 08, 2010 10:20 am
by AbraCadaver
In your example 'del' is the index, so what exactly to you want to access? If you did this:
Then try this to test:
Re: Output Array Indexes
Posted: Thu Jul 08, 2010 12:36 pm
by spacebiscuit
$_SESSION
is a 2 dimensional array. So if I print the variable I get a dump of the entire array. Instead I want to access each index ie:
However this would give me 'r' - the 2nd letter of 'Array'.
Rob.
Re: Output Array Indexes
Posted: Thu Jul 08, 2010 12:41 pm
by AbraCadaver
robburne wrote:$_SESSION
is a 2 dimensional array. So if I print the variable I get a dump of the entire array. Instead I want to access each index ie:
However this would give me 'r' - the 2nd letter of 'Array'.
Rob.
Not unless $_SESSION['del'] = "array";
Give a print_r() of $_SESSION['del'] so we can see.
Re: Output Array Indexes
Posted: Sun Jul 11, 2010 7:37 am
by spacebiscuit
As requested;
Gives:
Code: Select all
Array( [0] => 2 [1] => John Smith [2] => A Company [3] => 1 High St [4] => [5] => Anytown [6] => Anywhere [7] => AB1 2CD [8] => UK [9] => 1234567890 [10] => email@email.com [11] => john [12] => smith [13] => 263 [14] => UK - Mainland [15] => 10.46 [16] => Goods [17] => 1 [18] => 2 [19] => 2 [20] => 0)
Yet
Gives:
I can't figure it out, any ideas?
Thanks,
Rob.
Re: Output Array Indexes
Posted: Fri Jul 16, 2010 11:40 am
by spacebiscuit
Hi guys - I just wanted to bump this as I still have not solved his issue which makes no sense.
To recap, I am sending a multi-dimensional array in web form to a 3rd party payment processing script as follows:
Code: Select all
<input type="hidden" name ="success_url" value="http://www.paymaster.com/index.php?mydata=<? print_r($_SESSION[del]); ?>">
When the payment is successful the the user is re-directed to the success page. I am testing the array has been passed successfully with:
Which outputs the whole array:
Code: Select all
Array( [0] => 2 [1] => John Smith [2] => A Company [3] => 1 High St [4] => [5] => Anytown [6] => Anywhere [7] => AB1 2CD [8] => UK [9] => 1234567890 [10] => email@email.com [11] => john [12] => smith [13] => 263 [14] => UK - Mainland [15] => 10.46 [16] => Goods [17] => 1 [18] => 2 [19] => 2 [20] => 0)
So far so good, but I only want to access the indexes one by one, so for example:
But instead of outputting '2' it outputs the first letter of array: 'A'.
This makes no sense at all - any experts care to help?
Thanks in advance,
Rob.
Re: Output Array Indexes
Posted: Fri Jul 16, 2010 2:14 pm
by AbraCadaver
I see now. You are not passing the array, you are passing the string representation of the array. Try this:
Code: Select all
$mydata = urlencode(serialize($_SESSION['del']));
Code: Select all
<input type="hidden" name ="success_url" value="http://www.paymaster.com/index.php?mydata=<? echo $mydata; ?>">
Then on the next page:
Code: Select all
$mydata = unserialize($_GET['mydata']);
print_r($mydata);
echo $mydata[0];
You could also use json_encode() instead of serialize() I think.
Not sure how you will use this, but http_build_query() might be a way to go as well.
Re: Output Array Indexes
Posted: Sun Jul 18, 2010 11:17 am
by spacebiscuit
Thanks for the rpely. I am managing to make progrss of sorts.
If I use the unserialise function and then print the array the output is empty. However if I remove the unserialise function and just output the array, ie.
Code: Select all
a:21:{i:0;s:1:\"2\";i:1;s:9:\"John Smith\";i:2;s:12:\"A Company\";i:3;s:24:\"1 High St\";i:4;s:0:\"\";i:5;s:7:\"Somewhere\";i:6;s:6:\"A Town\";i:7;s:7:\"Post Code\";i:8;s:14:\"UK\";i:9;s:13:\"1234567890\";i:10;s:20:\"johnsmith@email.com\";i:11;s:8:\"johnsmith\";i:12;s:7:\"xxx\";i:13;s:3:\"275\";i:14;s:13:\"UK - Mainland\";i:15;s:5:\"10.46\";i:16;s:5:\"stuff\";i:17;s:4:\"1.00\";i:18;s:2:\"16\";i:19;s:1:\"2\";i:20;s:1:\"0\";}
I have tried playing with the unserialize and urldecode functions without success.
Any suggestions.
Thanks,
Rob.
Re: Output Array Indexes
Posted: Sun Jul 18, 2010 11:52 am
by AbraCadaver
It looks like you have magic quotes enabled. Try this:
Code: Select all
$mydata = stripslashes(unserialize($_GET['mydata']));
print_r($mydata);
Re: Output Array Indexes
Posted: Sun Jul 18, 2010 12:00 pm
by spacebiscuit
Awesome!
Thanks guys, finally got it working now. Not sure how something so simple took so long to solve but at least it is done now - solved!
One last question can I shoten this :
Code: Select all
<?
$mydata = urlencode(serialize($_SESSION['del']));
?>
<input type="hidden" name ="success_url" value="http://www.paymaster.com/index.php?mydata=<? echo $mydata; ?>">
ie. can I put the serialize in the echo of the form variable declaration?
Rob.