开发者

PHP display problem?

开发者 https://www.devze.com 2022-12-13 22:57 出处:网络
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.

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 thi开发者_如何学编程s 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];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号