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?

2
  • Do you want - as the result or do you want -, -, -? Commented Oct 25, 2016 at 18:45
  • @AbraCadaver your answer is right, I will accept that one although both were helpful hard decision for the voting. Commented Oct 25, 2016 at 18:47

2 Answers 2

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 : '-';
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Those should both work for 5.3. Do you get an error?
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
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.