Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an array is empty in PHP(Codeigniter)

Tags:

php

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 .

like image 203
Granit Zhubi Avatar asked Mar 17 '26 03:03

Granit Zhubi


1 Answers

if (empty($data))
{
    echo "array is empty";
}
else
{
    echo "not empty";
} 

or count($data) returns the size of array.

If you are not sure about the variable being an array, you can check type and then size.

if(is_array($data) && count($data)>0)
like image 129
Danyal Sandeelo Avatar answered Mar 19 '26 01:03

Danyal Sandeelo