0

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

3
  • If id's are 31 and 32, do you want to multiply their stocks? Like stock of 31 * stock of 32? Simply multiplying 31*32 makes no sense Commented Sep 29, 2016 at 17:07
  • @ObjectManipulator typo man. Commented Sep 29, 2016 at 17:08
  • print_r($arr_product_values ); and you'll find your array isn't what you expect Commented Sep 29, 2016 at 17:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.