2

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 .

2
  • Are there issues with your current code? Commented Jun 19, 2015 at 11:24
  • You have: if (!empty($data)) {... which is a test which is true when it has values but you then report it as being empty. Use: if (empty($data)) {... instead. i.e. remove the 'not' ('!') operator. Commented Jun 19, 2015 at 17:43

3 Answers 3

7
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)
Sign up to request clarification or add additional context in comments.

Comments

3

You can do this way also

if(is_array($data) && count($data)>0)
{
   echo "not empty";
}else{
   echo "empty";
}

Comments

0

In your code, you are checking if the $data array is empty after loading it with coordinates from the model. However, you are checking it immediately after loading the view, which means the $data array will never be empty at that point. Instead, you should check if the array is empty before trying to use it. Here's an updated version of your 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);

    if (empty($data['coordinates'])) {
        echo 'Array is empty';
    } else {
        $this->load->view('road/show', $data);
    }
}

In this updated code, I've changed the condition to check if $data['coordinates'] is empty before loading the view. This way, you'll be able to handle the case when the array is empty before trying to use it in the view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.