-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!

hiral2
  • 194
  • 1
  • 5
Mitzayapa
  • 19
  • 3

3 Answers3

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • now i have `Array ( [0] => Array ( [GROUP_CONCAT(gift_product)] => 6475,7377,6367 ) )` how do i print only 6475,7377,6367? – Mitzayapa Nov 04 '19 at 14:17
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))
Mojo Allmighty
  • 793
  • 7
  • 18
0

Use array_column to fetch column wise data with implode,

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

Demo

Output

6475,7377,6367
Rahul
  • 18,271
  • 7
  • 41
  • 60