4

I am trying to get the following output:

string1, string2, string3

Those values comes from $var1, $var2 and $var3 but they can be NULL at some point and here is where my problem is.

So far this is what I have:

$arr = array(
    $var1 !== null ? $var1 : '',
    $var2 !== null ? $var2 : '',
    $var3 !== null ? $var3 : '',
);

echo $arr !== '' ? implode(', ', $arr) : '-';

This are the test I've run:

input: array('string1', 'string2', 'string3')
output: string1, string2, string3

input: array('string1', 'string2')
output: string1, string2

input: array('', '', '')
output: , ,

input: array(null, null, null)
output: , ,

As you may notice if values are coming everything work as I want, if values are coming NULL then I am getting , , when I what I want is just a -.

I have tried to find whether the array contains empty values or not using this code:

$cnt = count($arr) !== count(array_filter($arr, "strlen"));
echo $cnt;

Then I've run the following test:

input: array('string1', 'string2', 'string3')
output: 3

input: array('string1', 'string2')
output: 2

input: array('', '', '')
output: 1

input: array(null, null, null)
output: 1

What I am missing or doing wrong here? How I can achieve this?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363

2 Answers2

3

If you want -, then this is wrong:

echo $arr !== '' ? implode(', ', $arr) : '-';
     ^^^^^^^^^^^

An array is not a string, and if you compare an array to a string, the array gets cast to a string, and turns into the literal word Array. That means you're doing

echo 'Array' !== '' ? ...

and of course, those aren't equal and you end up going down the implode path.

You have to test the individual values of the array for null, and then decide what to do. And note that array_count_values() won't work here - it can only count string/integer values. null as a value isn't countable.

php > $arr = array(null, null);
php > var_dump(array_count_values($arr));
PHP Warning:  array_count_values(): Can only count STRING and INTEGER values! in php shell code on line 1
PHP Warning:  array_count_values(): Can only count STRING and INTEGER values! in php shell code on line 1
array(0) {
}

So you have to do it the hard way:

$cnt = 0;
foreach($arr as $key => $value) {
    if (is_null($value)) { $cnt++; }
}
if ($cnt == count($arr)) {
   ... array is all nulls
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
3

Filter the array before implode and if the imploded array is an empty string assign -, otherwise assign the imploded array:

$result = implode(', ', array_filter($arr)) ?: '-';

For PHP < 5.3.0 not supporting ?: then:

$result = ($s = implode(', ', array_filter($arr))) ? $s : '-';
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Ups, that last one for *older version* isn't working. I am using PHP 5.3.10 (don't kill me isn't me, is company rules) in development server. – ReynierPM Oct 25 '16 at 18:52
  • Those should both work for 5.3. Do you get an error? – AbraCadaver Oct 25 '16 at 18:54
  • No, I get `-` for all of them, even if values are coming. Both? You mean the first one should work as well? The problem is I am developing in a Docker container using PHP 5.5.9 but the development server has PHP 5.3.10 so nvm I will make a few test and I will be back if they aren't working – ReynierPM Oct 25 '16 at 18:55