Hello
Pretty new to .php and am really struggling getting my head around splitting this array up to do what I want…
My array is from the URL here is an example
the array I get is something like
112,113,114,115,116:40,24,34,24,12,12
Where the first 6 numbers prior to the : are products and the numbers after the: are quantities.
Below is the code I have for adding these
<?
require('includes/application_top.php');
// echo $products;
$ids = explode(",", $products);
if (is_array($ids)) {
foreach ($ids as $id){
$cart->add_cart($id, $cart->get_quantity((int)$id)+1);
}
}
tep_redirect(tep_href_link('shopping_cart.php'));
?>
I want to split the array and have the quantities in the part where at the moment the code is “+1”
I cannot get any attempts to work and any help would be very greatly appreciated.
Cheers
Henry
You’d be better off splitting by the colon first, then by comma:
list($ids, $quantities) = explode(':', $products);
$ids = explode(',', $ids);
$quantities = explode(',' $quantities);
foreach ($ids as $key -> $value) {
$cart->add_cart($value, $cart->get_quantity((int)$id) + $quantities[$key]);
}
No need to do is_array, as explode will certainly return an array.
Hello
Thank you. That is pretty much what I have been trying to do.
It is spitting out this error now
Warning: Illegal offset type in /homepages/2/d313747421/htdocs/buy_now.php on line 10
:sick:
<?
require(‘includes/application_top.php’);
// echo $products;
list($ids, $quantity) = explode(‘:’, $products);
$ids = explode(‘,’, $ids);
$quantity = explode(‘,’, $quantity);
foreach ($ids as $key -> $value)
{ $cart->add_cart($value, $cart->get_quantity((int)$id) + $quantity[$key]);
}
tep_redirect(tep_href_link(‘shopping_cart.php’));
?>
Array s and object s can not be used as keys. Doing so will result in a warning: Illegal offset type.
Is there a way round this?
any help would be amazing…
hash
4
foreach ($ids as $key => $value)
That is fantastic thank you very much.
Cheers
Henry