How to concatinate square bracket to text only if array is not empty | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to concatinate square bracket to text only if array is not empty

Hallo, I have an Array that is soetimes empty and sometime have lalues. After extraction I want the answer to be in square bracket. So how to add those only if the Array habe Elements? If I use this: $a . "[ " , $b ."]". It is ok if the Array have Elements, but if the Array is emoty i get this Output: [] Please help https://code.sololearn.com/wn12f6tlvmFr/#php

3rd Apr 2020, 10:52 PM
pap
pap - avatar
2 Answers
+ 3
You may test if your result is empty... as you do your code, the simplest is to change the last expression: $final = $z . ($b ? " [" . $b . "]" : ""); that's a ternary operator (an if-else shortcut) wich say to concat to $z an empty string if $b is empty, else concat $b wrapped in square brackets.
3rd Apr 2020, 11:46 PM
visph
visph - avatar
0
visph thank you! it works like charm!!!
4th Apr 2020, 6:22 AM
pap
pap - avatar