Mujahid Mujahid - 3 months ago 6
PHP Question

How to add a loop content to an array in php

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

Answer
$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

Comments