roger
roger

Reputation: 33

Array elements not showing?

I am trying to print the [0] element of an array. The code below creates my array but the output just says 'Array'

$this->db->select('company_name');
$query = $this->db->get('companies');
$query = $query->result_array();
echo $query['company_name'];

Upvotes: 0

Views: 6006

Answers (4)

Ximik
Ximik

Reputation: 2495

$query['company_name'] is an array.

Use print_r($query['company_name']); to see more details.

Upvotes: 1

Rebecca Nelson
Rebecca Nelson

Reputation: 1296

What you are trying to do is echo an Array itself. If you just try to echo an Array, PHP will not show you the contents.

When you get results from a database, typically it will be a two dimensional array, that is, it will be an array whose data is more arrays. If you echo the data in the top-most array's index 1, you will be trying to echo an array, since that is what is located in the first array.

In order to actually get the contents, you need to use two indexes; one for the index of the array, and one for the index of the data in that array:

<?php
    $my_array = array(array(8, 2, 4), array(7, 12, 32), array(62, 2, 1));
    echo $my_array[1][1] // echos 12
    echo $my_array[1] // echos "Array" because the data at index 1 is an array
?>

Or maybe you want to see all the contents of the array. If so, the print_r() function is your friend. It is recursive and lets you see the contents of arrays inside of arrays:

<?php
    $my_array = array(array(8, 2, 4), array(7, 12, 32), array(62, 2, 1));
    print_r($my_array);
    /*
        The above will output the following:
        Array
        (
            [0] => Array
                (
                     [0] => 8
                     [1] => 2
                     [2] => 4
                )

            [1] => Array
                (
                     [0] => 7
                     [1] => 12
                     [2] => 32
                )

            [2] => Array
                (
                     [0] => 62
                     [1] => 2
                     [2] => 1
                )
        )
    */
?>

Check out the reference at the print_r() PHP Reference for more information.

Upvotes: 0

SW4
SW4

Reputation: 71170

$query['company_name'] is an array, this array consists of the value for the 'company_name' field for each record in the returned resultset,

$query['company_name']['0'] is therefore the company_name for in the first returned row.

Therefore you want either:

echo $query['company_name'][0];
print $query['company_name'][0]
print_r($query['company_name']);

Simply doing:

print_r($query);

Will give you an idea of how the returned resultset is structured.

Upvotes: 0

Grofit
Grofit

Reputation: 18465

Havent used PHP for a while but do something like:

print_r($query);

That will display everything from that object, then check to see if the index you are looking at is an array itself,as you can only really echo the value of something if it holds a single value opposed to a list of values.

Upvotes: 1

Related Questions