如何在 PHP(Codeigniter) 中检查数组是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30936919/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:08:42 来源:igfitidea点击:
How to check if an array is empty in PHP(Codeigniter)
提问by Granit Zhubi
Here it's my sample of code :
这是我的代码示例:
public function show($imei,$start_time,$end_time,$dateto) {
$from_time = str_replace('-','/',$start_time);
$fromi=strtotime($from_time . ' ' . $end_time);
$too1=strtotime($from_time . ' ' . $dateto);
$data['coordinates'] = $this->road_model->get_coordinatesudhetime($imei, $fromi, $too1);
$this->load->view('road/show', $data);
if (!empty($data))
{
echo 'Array it's empty*';
}
}
I want to check when $data it's empty .
我想检查 $data 何时为空。
回答by Danyal Sandeelo
if (empty($data))
{
echo "array is empty";
}
else
{
echo "not empty";
}
or count($data)
returns the size of array.
或count($data)
返回数组的大小。
回答by Always Sunny
You can do this way also
你也可以这样做
if(is_array($data) && count($data)>0)
{
echo "not empty";
}else{
echo "empty";
}