-3

How do i print array values from this query ? I need something like 6475,7377,6367. (comma separated).

This is what i get when i do a print_r($myarray):

    Array
    (
        [0] => Array
            (
                [gift_product] => 6475
            )

        [1] => Array
            (
                [gift_product] => 7377
            )

        [2] => Array
            (
                [gift_product] => 6367
            )

    )

Thanks alot!

0

3 Answers 3

0

You could have handled this on the MySQL side using GROUP_CONCAT, something like this:

SELECT GROUP_CONCAT(gift_product) AS products
FROM yourTable
GROUP BY id;

Your current output indicates that you are getting back gift_product values over several records, when in fact you wanted to retrieve a CSV string of products.

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

1 Comment

now i have Array ( [0] => Array ( [GROUP_CONCAT(gift_product)] => 6475,7377,6367 ) ) how do i print only 6475,7377,6367?
0

You can use implode function after you map your array using array_map:

echo implode(', ', array_map(function ($entry) {
  return $entry['gift_product'];
}, $myarray))

Comments

0

Use array_column to fetch column wise data with implode,

echo implode(",", array_column($arr, "gift_product"));

Demo

Output

6475,7377,6367

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.