Array elements not showing?

By : roger
Source: Stackoverflow.com
Question!

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'];
By : roger


Answers

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.

By : Grofit


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:

By : TF Nelson


$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.

By : SW4


This video can help you solving your question :)
By: admin