I am working with php CodeIgniter and I am getting results from a query which I save in the following array
$arr = array();
foreach ($query->result() as $row)
{
$arr[] = array("id"=>$row->id, "nom"=>$row->nombre);
}
But I haven't found how to iterate through the array in order to use the "id" and "nom" values of each index.
You could use to get the result of the query, with $result being what you got from the query:
Then it is easier to get the fields because you use:
This way you easily extract the values returned in the query
SQL
."id"
and"nombre"
it is just as the fields are called in your table, so if you have a field in your table called "surname" to obtain each of the indices of the result of the query,"apellido"
you should do:That is the solution Greetings!
If you store your data in an array like this:
You can access them one by one from this other:
What you are creating is a multidimensional array where each value is an associative array that stores the values obtained
$row->id
through$row->nombre
the query for each record, assigning them to the keys 'id' and 'nom'. Thus, to access a specific data, for example the ID of the third record obtained, you will have to access using first the position, to access the associative array and then the index associated with the ID data within it: