0

I've got an array which when I var_dump looks like this:

array(2) { [0]=> array(1) { ["unit_id"]=> string(1) "1" } [1]=> array(1) { ["unit_id"]=> string(1) "3" } }

I need to extract each of those values (1, 3) to where I can put them in a SQL WHERE IN clause

So far I've found the php implode function but I do not know how to do that on a multidimensional array.

Please help :(

jasonayer
  • 41
  • 1
  • 1
  • 4

2 Answers2

1

Try this code:

$arr = array(                       // your array
               array("unit_id"=>1),
               array("unit_id"=>3)
            );

$str = implode(',', array_map(function($el){ return $el['unit_id']; }, $arr));
Kumar V
  • 8,810
  • 9
  • 39
  • 58
0

If you are using php version >= 5.5, then try:

echo implode(",",array_column($arr,'unit_id'));

If not then try:

$res = array();   
foreach($arr as $ar){
    $res[] = $ar['unit_id'];
}

   echo implode(",",$res);

See demo here

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28