简体   繁体   中英

Replace null array value from array in php

I run a query and got id values in an array. Then I push those id like this array_push($id); then I implode ',' into an array and place it in a variable like this $val = implode(",",$id); . When I output it i get following result ,2,3,4,5,6,7,8 . Problem in this result is that first value is ',' and it is because first record that I am getting from database is giving me Null value. I want to remove ',' from first place if first record is empty. So that desire result should be 2,3,4,5,6,7,8 .

Please anybody can help me how can I remove ',' from first place. Or any suggestion to solve this problem

Do it like this:

$id = array_filter($id, 'ord'); // removes 0 length values, or
$id = array_filter($id, 'strlen'); // same as above but a bit slower, or
$id = array_filter($id, 'count'); // removes only NULL values and empty arrays

$val = implode(",",$id);

You might also want to consider using is_numeric as the array_filter callback.

Doing just array_filter($id) will remove falsy (like 0 ) elements that you might want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM