duanfu1873 2016-09-29 17:05
浏览 54

使用foreach将值放在数组中并获取值

e.g. I have this Product Id values 31,32 from database. I want to put them on array. So, I can use the values for foreach.

What I want to achieve: I want to get the stock of each product from database according to the given values (31,32).

What I tried:

$product_values = '31,32'; //this values can be different, sample values only
$arr_product_values = array();
$arr_product_values[] = $product_values;
foreach ($arr_product_values as $prod_id) {

  echo $prod_id;
 }

Expected output:

31 & 32

  • 写回答

1条回答 默认 最新

  • douejuan9162 2016-09-29 17:08
    关注
    $arr_product_values[] = $product_values; 
    

    That doesnt mean you have a new array with those 2 values now. You just took a comma separated string and assigned it to an array element, that doesnt make it an array itself.

    $arr_product_values = array(31,32);
    

    Does.

    Now you can loop over it

    评论

报告相同问题?