PHP array manipulation help

Hi everyone, I’m getting my head in a twist with something and I’d
appreciate a little help if anyone has the time…

I’m finishing off a little shopping site with a Paypal shopping cart
(driven by Mal’s of course), and I want to use the Paypal callback
feature to run a script that updates a MySQL database to mark items
as being sold.

The problem is how I go about handling the data that’s returned from
a completed Paypal sale. It comes in the following form:

Product name : 1 : 1.99 : 0 : 26

And all I care about is the last item, which is the record number.
Now, if someone buys more than one product in a transaction the data
that’s returned is delimited by a tilde, as in this example where two
items were bought in one go:

Product name : 1 : 1.99 : 0 : 26~Product name two : 1 : 2.50 : 0 : 27

I need to iterate through the data, whether it is one item or x
number of items, compiling a list of the record number from each one,
so I can then run my ‘sold’ script for each record number.

It is partly my general slight difficulty with visualising arrays
properly in my head and partly the fact that I’m somewhat distracted
by other things, but I just can’t see my way through this. Oh,
embarassing!

Can anyone see a simple way to do this, so I can get it sorted and
then spend time writing out “I must learn to handle arrays better”
1000 times?

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

In Javascript there’s an array.split(‘delimiter’) function and I’d be
surprised if there wasn’t one in PHP.

In fact, try this: http://theserverpages.com/php/manual/en/function.split.php

Joe

On 16 Nov 2007, at 20:18, Keith Martin wrote:

Hi everyone, I’m getting my head in a twist with something and I’d
appreciate a little help if anyone has the time…

I’m finishing off a little shopping site with a Paypal shopping cart
(driven by Mal’s of course), and I want to use the Paypal callback
feature to run a script that updates a MySQL database to mark items
as being sold.

The problem is how I go about handling the data that’s returned from
a completed Paypal sale. It comes in the following form:

Product name : 1 : 1.99 : 0 : 26

And all I care about is the last item, which is the record number.
Now, if someone buys more than one product in a transaction the data
that’s returned is delimited by a tilde, as in this example where two
items were bought in one go:

Product name : 1 : 1.99 : 0 : 26~Product name two : 1 : 2.50 : 0 : 27

I need to iterate through the data, whether it is one item or x
number of items, compiling a list of the record number from each one,
so I can then run my ‘sold’ script for each record number.

It is partly my general slight difficulty with visualising arrays
properly in my head and partly the fact that I’m somewhat distracted
by other things, but I just can’t see my way through this. Oh,
embarassing!

Can anyone see a simple way to do this, so I can get it sorted and
then spend time writing out “I must learn to handle arrays better”
1000 times?

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Keith,

One of the ways of skinning it:

<?php // Your passed string, you need to grab this into $prod_str $prod_str = 'Product name : 1 : 1.99 : 0 : 26~Product name two : 1 : 2.50 : 0 : 27'; $array_str1 = array(); $array_str1 = explode("~",str_replace(' ','',$prod_str)); // Explode the prod_str string after removing any spaces $as1_count = count($array_str1); // How many items are in the array $array_str2 = array(); for ($i = 0 ; $i < $as1_count ; $i++) { // Run through the array for the number of product strings it contains $array_str2 = explode(":",$array_str1[$i]); // Explode the individual product strings // Do what you need to with the product ID - $array_str2[4] // e.g. echo $array_str2[4] . '
'; // show the 5th item in the product string } ?>

Note that this always returns the 5th item, so if the ID is not always
the 5th item the code would need to be changed by maybe counting the
items in $array_str2 and gabbing the last. You could also make a
function that would return an array, so you would then ned to handle
each array value for the id if existing, if you want any help on that
then just let me know.

Does this help?
Mike

On Nov 16, 2007, at 9:18 PM, Keith Martin wrote:

Hi everyone, I’m getting my head in a twist with something and I’d
appreciate a little help if anyone has the time…

I’m finishing off a little shopping site with a Paypal shopping cart
(driven by Mal’s of course), and I want to use the Paypal callback
feature to run a script that updates a MySQL database to mark items
as being sold.

The problem is how I go about handling the data that’s returned from
a completed Paypal sale. It comes in the following form:

Product name : 1 : 1.99 : 0 : 26

And all I care about is the last item, which is the record number.
Now, if someone buys more than one product in a transaction the data
that’s returned is delimited by a tilde, as in this example where two
items were bought in one go:

Product name : 1 : 1.99 : 0 : 26~Product name two : 1 : 2.50 : 0 : 27

I need to iterate through the data, whether it is one item or x
number of items, compiling a list of the record number from each one,
so I can then run my ‘sold’ script for each record number.

It is partly my general slight difficulty with visualising arrays
properly in my head and partly the fact that I’m somewhat distracted
by other things, but I just can’t see my way through this. Oh,
embarassing!

Can anyone see a simple way to do this, so I can get it sorted and
then spend time writing out “I must learn to handle arrays better”
1000 times?

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Very strange the way this thread is structured, it seems Keith’s original post is not visible but Joe’s is shown twice! also the lines of PHP code and comments within the for loop have all been written on one line and not on 4 as was sent…


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yeah that is odd.

Also just noticed the function I gave you splits a string not an
array…sorry :blush:

On 16 Nov 2007, at 21:22, Mike B wrote:

Very strange the way this thread is structured, it seems Keith’s
original post is not visible but Joe’s is shown twice! also the
lines of PHP code and comments within the for loop have all been
written on one line and not on 4 as was sent…


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Don’t know about posts but I used a tab at the start of each line in
the for loop, maybe this is being interpreted in some weird way and
making the lines all one?

On Nov 16, 2007, at 10:32 PM, Joe Billings wrote:

Yeah that is odd.

Also just noticed the function I gave you splits a string not an
array…sorry :blush:

You could actually use split() as the following to give the same
results:

<?php $prod_str = 'Product name : 1 : 1.99 : 0 : 26~Product name two : 1 : 2.50 : 0 : 27'; $array_str1 = array(); $array_str1 = split("[~]",str_replace(' ','',$prod_str)); // Explode the prod_str string after removing any spaces $as1_count = count($array_str1); // How many items are in the array $array_str2 = array(); for ($i = 0 ; $i < $as1_count ; $i++) { // Run through the array for the number of product strings it contains $array_str2 = split("[:]",$array_str1[$i]); // Explode the individual product strings // Do what you need to with the product ID - $array_str2[4] // e.g. echo $array_str2[4] . '
'; // show the 5th item in the product string } ?>

On 16 Nov 2007, at 21:22, Mike B wrote:

Very strange the way this thread is structured, it seems Keith’s
original post is not visible but Joe’s is shown twice! also the
lines of PHP code and comments within the for loop have all been
written on one line and not on 4 as was sent…


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sometime around 16/11/07 (at 21:32 +0000) Joe Billings said:

Also just noticed the function I gave you splits a string not an
array…sorry :blush:

:slight_smile:

No problem Joe, thanks for the attempt! (And the original data wasn’t
an array to start with.)

Mike, thanks very much for the chunk o’ code, it has given me a major
kick-start. (Aka ‘kick up the backside…’)
I’m experimenting now to see how best to weave it into things. I’ll
see how it goes.

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options