The word Array is displayed instead of the databas

2019-09-09 19:19发布

问题:

I am trying to print selected product names from my database, however all i see is the word "Array" instead of the product name. It is displayed as many times as the number products it finds, and that number is correct, but it only shows "Array"...

Here's my code:

public function lookup($product)
    {

        $camera = $this->db->query("SELECT name FROM products WHERE type = 'Camera' ");

        $laptop = $this->db->query("SELECT name FROM products WHERE type = 'Laptop' ");


        $data = array();

        switch($product)
        {
            case 'camera' : 
            foreach ($camera->result() as $row)
            {
                $entry = array();
                $entry['name'] = $row->name;
                $data[] = $entry;
            }
            return $data; break;

            case 'laptop' :
            foreach ($laptop->result() as $row)
            {
                $entry = array();
                $entry['name'] = $row->name;
                $data[] = $entry;
            }
            return $data; break;

        }   
    }

回答1:

Are you trying to echo an array? You should probably try var_dump($array_var); or use a loop such as foreach to traverse the array values and print/echo each one.