bernhard Posted May 25, 2016 Posted May 25, 2016 i'm sure this is nothing fancy for all the pros here, but at least i did that a lot more inconvenient for a long time and maybe it will help someone else... $lines = array(); $lines[] = $page->company; $lines[] = $page->forename . ' ' . $page->surname; $lines[] = $page->address; $lines[] = $page->zip . ' ' . $page->city; $lines[] = $page->country; echo implode("<br>", array_filter($lines)); // or like this $lines = array( $page->company, implode(" ", array_filter(array( $page->prefix, $page->forename, $page->surname, $page->suffix ))), $page->address, $page->zip . ' ' . $page->city, $page->country ); echo implode("<br>", array_filter($lines)); it will skip all empty lines automatically (for example if there is no "company" or no country) and it's a whole more readable then with lots of IFS and ECHOS 7
tpr Posted May 25, 2016 Posted May 25, 2016 Nice! Have you considered adding microformats somehow (eg spans)?
kongondo Posted May 25, 2016 Posted May 25, 2016 Probably not relevant in your case, but just an FYI, array_filter considers zero values as empties...Will be a gotcha in use cases where you actually want zeroes 2
bernhard Posted May 25, 2016 Author Posted May 25, 2016 Nice! Have you considered adding microformats somehow (eg spans)? i dont need any because i'm rendering it for a PDF; but would be a valid point can you use this for a form ? i don't understand your question. what do you mean? Probably not relevant in your case, but just an FYI, array_filter considers zero values as empties...Will be a gotcha in use cases where you actually want zeroes thanks for the hint and link
bernhard Posted July 26, 2016 Author Posted July 26, 2016 here you have a working example respecting 0 values: https://eval.in/612114 taken from http://stackoverflow.com/questions/35527457/array-filter-skip-null-or-0-value 2
LostKobrakai Posted July 26, 2016 Posted July 26, 2016 And if you want a bit more nicer syntax consider this one: https://github.com/tightenco/collect https://laravel.com/docs/master/collections 2
bernhard Posted July 26, 2016 Author Posted July 26, 2016 1 hour ago, LostKobrakai said: And if you want a bit more nicer syntax consider this one: https://github.com/tightenco/collect https://laravel.com/docs/master/collections and a link to your previously recommended collections book. on the second look it makes even more sense still a lot to learn
Recommended Posts