tags:

views:

40

answers:

2

For some reason when an array has for example, four values it will display all four values four times I just want the values to be displayed one time.

How can I fix this problem? Note the first echo works perfectly.

Here is the code.

if (count($array) == 1){
 echo $array[$x] . " one value has been entered";
} else {
 echo implode(", ", $array) ." you entered more then one value;

}
+3  A: 

Because $x obviously isn't the index of the first element of the array. Use the correct index. Or if you don't know what it is, just use reset():

if (count($array) == 1) {
  echo reset($array) . ' one value has been entered';
} else {
  echo implode(', ', $array) . ' you entered more than one value';
}

It might be helpful to dump the array to see what it actually contains:

print_r($array);
cletus
this still does not solve the problem.
FukHaLfDaN
I fail to see how. To put it another way: what's happening? Or another: I suspect you have something else going awry.
cletus
let say the array has two value apple and pear it will display each value twice apple pear apple pear.
FukHaLfDaN
If so it's doing that with code you haven't presented here so, like I said, you've got something else going on. Perhaps you're not building/seeding $array correctly. Perhaps this section of code is being called more than once. Something like that.
cletus
Your comment made me take a second look at my code which helped me solve my own question.
FukHaLfDaN
A: 

$x is not set in your code or just meaningless. If you have just one array item you can print it with the simple echo $array[0];

markmywords
Thanks, but this still dosn't solve my problem.
FukHaLfDaN