Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count arrays

Status
Not open for further replies.

yebaws

Technical User
Mar 22, 2007
42
GB
I'm still learning this stuff, so please bear with me...

I have a shopping cart which returns a variable on checkout completion containing the cart contents. The variable looks like this:

product1 : quantity1 : price1 ~ product2 : quantity2 : price2

ie: parameters for each item in the cart delimited by : and each cart item delimited by ~

What I want to do is run a fairly complex routine on the cartcontents variable, which will involve breaking it down in to each product and then breaking down the different parameters of each product in order to update the product database and the shoppers account.

My plan is to make a function for the routine:

function processcart($product){
<<function contents>>}

split the variable into an array (using preg_split) thus:

$values = preg_split("/~/", "$cart");

and then run the function on each value in the array.

$product1 = $values[0];
function processcart($product1);

$product2 = $values[1];
function processcart($product2);

etc....(not sure if this is the simplest way to do it!)

The problem is, I don't know how many items (and therefore how many variables in the split cartcontents) there will be in the cart each time.

I realise that I could use count():

$noofproducts = count($values);

to work out the number of products, but I'm not sure how I can then use this number to get my routine to run the necessary number of times (and also increment the variable / array number - ie "$product1 = $values[0]" becomes "$product2 = $values[1]" each time it runs)

Thanks in advance...
 
Welcome to the wonderful world of loops. Either for loops, or even while loops would work for what you want.

As a side note, I'd use the explode() function rather than preg-split as it is faster, and since you are only looking for a single character, running the regular expression engine seems like overkill.

Anyway, for the loop yes you'll want to explode the original string into its parts, and then simply explode it again for the product specifications.

Code:
$tempArray=explode("~",$cart);
for($i=0;$i<=count($tempArray)-1;$i++){
$Products[]=explode(":",$tempArray[$i]);
}

You could even alter it to have the key be the product name if you needed to.
At then the $Products array will contain sub arrays with the products names prices and quantities you can loop through.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks for that and the tip about explode.

Before I start testing this, from what I can see, if I run your code on the following cart contents:

product1 : quantity1 : price1 ~ product2 : quantity2 : price2

I'll get

$Products[0] = "product1"
$Products[1] = "quantity1"
$Products[2] = "price1"
$Products[3] = "product2"
$Products[4] = "quantity2"
$Products[5] = "product3"

is that correct?
 
Nope, you'll get a multidimensional array such as:

$Products[0][0]="Product1"
$Products[0][1]="Quantity1"
$Products[0][2]="Price1";
$Products[1][0]="Product2"
$Products[1][1]="Quantity2"
$Products[1][2]="Price2";
$Products[2][0]="Product3"
$Products[2][1]="Quantity3"
$Products[2][2]="Price3";
...


You could of course alter the array to be more defined if you wanted to such as:

Code:
for($i=0;$i<=count($tempArray)-1;$i++){
$tmpArr=explode(":",$tempArray[$i]);
$Products[$tmpArr[0]]['product']=$tmpArr[0];
$Products[$tmpArr[0]]['quantity']=$tmpArr[1];
$Products[$tmpArr[0]]['price']=$tmpArr[2];

This would produce something like:

Code:
$Products['product1']['product']="Product1"
$Products['product1']['quantity']="Quantity1"
$Products['product1']['price']="Price1";
$Products['product2']['product']="Product2"
$Products['product2']['quantity']="Quantity2"
$Products['product2']['price']="Price2";
$Products['product3']['product']="Product3"
$Products['product3']['quantity']="Quantity3"
$Products['product3']['price']="Price13";






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top