Reputation: 1237
In my current project, I need to add a loop contents to an array where I can use it later. This is my code. I tried some way but they're not working. Can anybody give a help to fix it:
for($i=0;$i<$max;$i++) {
$pid = $_SESSION['cart'][$i]['productid'];
$q = $_SESSION['cart'][$i]['qty'];
$pname = get_product_name($pid);
if($q == 0) {
continue;
} else {
$j = $i+1;
}
I need to add the $pid
to an array where I should be able to use implode(",", $pid)
Thanks
Upvotes: 1
Views: 117
Reputation: 4197
I would suggest you initialize an array for your pids
$arr_pids = array()
and each time you want to add a pid to this array just use
array_push($arr_pids, $pid)
Upvotes: 0
Reputation: 26791
$pids = array();
foreach ($_SESSION['cart'] as $cart)
{
$pids[] = $cart['productid'];
}
This will get you a $pids array.
Upvotes: 0
Reputation: 13435
Do you just mean this?
$pids = array();
for($i=0;$i<$max;$i++)
{
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
if($q==0)
{
continue;
}
// optimization... don't do anything if quantity is 0.
$pids[] = $pid;
$pname=get_product_name($pid);
}
echo implode(',', $pids);
Upvotes: 2
Reputation: 5520
$pids=array();
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$pids[]=$pid;
$q=$_SESSION['cart'][$i]['qty'];
$pname=get_product_name($pid);
if($q==0){
continue;
}else{
$j = $i+1;
}
}
echo implode(' - ',$pids);
You should be a little more clear about what your end result should be, I could be a bit more specific
Upvotes: 1
Reputation: 43840
initialiaze $pid as an array first
$pid = array();
Now in your loop add the values to it
$pid[] =$_SESSION['cart'][$i]['productid'];
note the square brackets with pid
after your loop you can extract the values from $pid
$someValue = $pid[0] * something';
I hope this is what u are looking for
Upvotes: 0