简体   繁体   中英

PHP display problem?

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;

}

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);

$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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM