0

how can i fetch data in an arrays with gives null value if null

here is my data I var_dump($showStatus); I want to print out . $showStatus[0]['title']

string(0) ""
    array(2) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "1"
        ["container_id"]=>
        string(1) "3"
        ["title"]=>
        string(51) "waitting"
      }
      [1]=>
      array(7) {
        ["id"]=>
        string(1) "2"
        ["container_id"]=>
        string(1) "3"
        ["title"]=>
        string(72) "getting"
      }
    }
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "4"
        ["container_id"]=>
        string(1) "7"
        ["title"]=>
        string(51) "getting"
      }
    }

The reason that I've string because in my models I want to print "" or NULL when it don't have data here is my models

public function showStatus($id){
    $sql = 'SELECT * FROM status WHERE container_id = '.$id;
    if($this->query_rows( $sql )) {
        return $this->query_rows($sql);
    } else {
        return "";
    }
 }

I try to use

foreach ($getListData as $k) {

}

but it said Warning: Invalid argument supplied for foreach()

1
  • can you just print_r($showStatus); and check the array . Commented Jan 27, 2018 at 5:15

1 Answer 1

1

Try this:

if(!empty($getListData) )
{
    foreach ($getListData as $k) {
       print_r($k);
    }    
}
else {
    echo "NULL";
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you , so i've to check !empty first before I use foreach lopp right ?

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.