How can I check and see how many values I have in an array? For example I'd like to show something if my array is like this:
Array ( [0] => 1 [1] => 2 [2] => 3 )
but I'd like to hide it if my array only have one value:
Array ( [0] => 1 )
I guess I could do something like this:
if (in_array("2", $myArray)) { ... }
or a more generic approach:
if(1 > count($array)) { echo "There is a single value in the array"; } else echo "There are multiple values in the array";
cool
YAOMK wrote:or a more generic approach: if(1 > count($array)) { echo "There is a single value in the array"; } else echo "There are multiple values in the array";
Note that the if condition will be true if there is a single value or if there are no values or if the array is undefined (throwing a notice-level warning in the latter case).
NogDog wrote:Note that the if condition will be true if there is a single value or if there are no values or if the array is undefined (throwing a notice-level warning in the latter case).
Yeah sloppy coding on my part. Please have a look at this example instead:
<?php error_reporting(E_ALL); if(isset($array) && 1 >= count($array)) { echo "There is a single value in the array"; } elseif(isset($array)) echo "There are multiple values in the array"; ?>