2

I am facing some problem to fix this issue. My array same as below

Array ( [0] => Array ( [coupon] => dasd ) [1] => Array ( [coupon] => aaa ) ) 

From here I want to display only coupon value as comma separated. Same as below

dasd,aaa

To solve this I tried below code but that's not solving my issue.

<?php
$option_settings = array(
array(  'coupon'=>'dasd'     
    ),
array(  'coupon'=>'aaa')
);
print_r($option_settings );
echo $output = implode(',', $option_settings);
?>

Sample https://eval.in/784206

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67

4 Answers4

1

use simple array_column method

 $option_settings = array(
array(  'coupon'=>'dasd'     
    ),
array(  'coupon'=>'aaa')
);
print_r($option_settings );
$coupans_arr =array_column($option_settings, 'coupon');
echo $output = implode(",", $coupans_arr);

https://eval.in/784217

B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • Hi thanks for your answer . I tried your solution but it was displaying error in my wordpress system array_column undefined function. – Lemon Kazi Apr 28 '17 at 06:10
1

Try this code

    <?php
$option_settings = array(
array(  'coupon'=>'dasd'     
    ),
array(  'coupon'=>'aaa')
);


$arr = array();
foreach($option_settings as $opt) {
  $arr[] = $opt['coupon'];
}
echo $output = implode(',', $arr);
?>

Or You can use php function array_column()

$coupans_arr =array_column($option_settings, 'coupon');
echo $output = implode(",", $coupans_arr);

Live Demo

sandip kakade
  • 1,346
  • 5
  • 23
  • 49
0

This is what you want,

print_r(implode(',', array_column($array, 'coupon')));
LF00
  • 27,015
  • 29
  • 156
  • 295
-1

Please let me know if this works:

$option_settings = array(
array(  'coupon'=>'dasd'     
    ),
array(  'coupon'=>'aaa')
);

$result = '';


foreach($option_settings as $item)
{
    $result .= $item['coupon'] .', ';
}

$result = rtrim($result, ', ');

https://eval.in/784220

Biju P Dais
  • 131
  • 1
  • 12