arrays - PHP return nothing if array_column fields are blank -
the below code takes array, $array
, , array key, $item
, implodes adding comma each item. how can alter if value of item inside array null, implode not add blank comma.
public static function implode($array, $item) { return implode(',', array_column($array, $item)); }
for example:
$array = [ ['eri_number' => ''], ['eri_number' => '222'] ['eri_number' => ''] ]; $item = 'eri_number'; $myclass->implode($array, $item);
the above code output;
,222,
i want output 222
without other blank values.
can help?
you filter out empties using array_filter()
:
return implode(',', array_filter(array_column($array, $item)));
note filter out 0
, string 0
, false
, null
.
Comments
Post a Comment