php 如何确定一个数组是否有任何元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42226780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if an array has any elements or not?
提问by Sajid Mehmood
How do I find if an array has one or more elements?
如何确定一个数组是否有一个或多个元素?
I need to execute a block of code where the size of the array is greater than zero.
我需要执行一个数组大小大于零的代码块。
if ($result > 0) {
// Here is the code body which I want to execute
}
else {
// Here is some other code
}
回答by Dani
You can use the count()
or sizeof()
PHP functions:
您可以使用count()
或sizeof()
PHP 函数:
if (sizeof($result) > 0) {
echo "array size is greater than zero";
}
else {
echo "array size is zero";
}
Or you can use:
或者你可以使用:
if (count($result) > 0) {
echo "array size is greater than zero";
}
else {
echo "array size is zero";
}
回答by Mayank Pandeyz
count
— Count all elements in an array, or something in an object
count
— 计算数组中的所有元素,或对象中的某些内容
int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )
Counts all elements in an array, or something in an object.
计算数组中的所有元素或对象中的某些元素。
Example:
例子:
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3
In your case, it is like:
在你的情况下,它就像:
if (count($array) > 0)
{
// Execute some block of code here
}
回答by scaisEdge
You could avoid length retrieve and check using a simple foreach:
您可以使用简单的 foreach 来避免长度检索和检查:
foreach($result as $key=>$value) {
echo $value;
}
回答by rob006
回答by Vishal Solanki
@Sajid Mehmood in PHP we have count() to count the length of an array, when count() returns 0 that means that array is empty
@Sajid Mehmood 在 PHP 中我们使用 count() 来计算数组的长度,当 count() 返回 0 时表示数组为空
Let's take an example for your understanding:
举个例子让大家理解:
<?php
$arr1 = array(1); // With one value which will give 1 count
$arr2 = array(); // With no value which will give 0 count
// Now I want that the array which has greater than 0 count should print other wise not so
if (count($arr1)) {
print_r($arr1);
}
else {
echo "Sorry, array1 has 0 count";
}
if (count($arr2)) {
print_r($arr2);
}
else {
echo "Sorry, array2 has 0 count";
}
回答by shades3002
For those who start with the array in PHP presented it this way: more information here
对于那些以 PHP 中的数组开头的人,以这种方式呈现: 更多信息在这里
//Array
$result = array(1,2,3,4);
//Count all the elements of an array or something of an object
if (count($result) > 0) {
print_r($result);
}
// Or
// Determines if a variable is empty
if (!empty($result)) {
print_r($result);
}
// Or
// sizeof - Alias of count ()
if (sizeof($result)) {
print_r($result);
}
回答by mickmackusa
Pro Tip:
专家提示:
If you are sure that:
如果您确定:
- the variable exists (isset) AND
- the variable type is an array (is_array) ...might be true of all is_iterables, but I haven't researched that extension of the question scope.
- 变量存在(isset)AND
- 变量类型是一个数组 (is_array) ...可能适用于所有 is_iterables,但我还没有研究过问题范围的扩展。
Then you don't need to call any functions.An array with one or more elements has a boolean value of true
. An array with no elements has a boolean value of false
.
那么你不需要调用任何函数。包含一个或多个元素的数组的布尔值为true
。没有元素的数组的布尔值为false
。
Code: (Demo)
代码:(演示)
var_export((bool)[]);
echo "\n";
var_export((bool)['not empty']);
echo "\n";
var_export((bool)[0]);
echo "\n";
var_export((bool)[null]);
echo "\n";
var_export((bool)[false]);
echo "\n";
$noElements = [];
if ($noElements) {
echo 'not empty';
} else {
echo 'empty';
}
Output:
输出:
false
true
true
true
true
empty
回答by Muhammad Hamza
<pre>
$ii = 1;
$arry_count = count($args);
foreach ( $args as $post)
{
if( $ii == $arry_count )
{
$last = 'blog_last_item';
}
echo $last;
$ii++;
}
</pre>