Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access array in php by index based?

Tags:

php

I have a database table for User in that i have two fields USER_ID and USER_DESCRIPTION, If i run the bellow code i get array in the form.

Array ( [USER_ID] => 1 [USER_DESCRIPTION] => TAB ) 

But i want to access those value in index based like 0, 1.How to i get that.

while (($result = oci_fetch_array($data, OCI_ASSOC)) != false) {
    echo $result['USER_ID']. ' - ' .$result['USER_DESCRIPTION']; //This works
    echo $result[0]. ' - ' .$result[1]; //This is how i want to access the values
}
like image 684
Murali krishna Avatar asked Dec 23 '15 06:12

Murali krishna


People also ask

How do you access an array by index?

Access Array Elements Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How do you access array elements in PHP?

Accessing Elements in a PHP Array The elements in a PHP numerical key type array are accessed by referencing the variable containing the array, followed by the index into array of the required element enclosed in square brackets ([]).

What is PHP indexed array?

PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.

How do you access values in an array?

We can access elements of an array using the index operator [] . All you need do in order to access a particular element is to call the array you created. Beside the array is the index [] operator, which will have the value of the particular element's index position from a given array.


1 Answers

You have passed second parameter OCI_ASSOC to oci_fetch_array() which will fetch only associative array.

If you change that parameter to OCI_BOTH, it will return both numeric as well associative array.

OCI_BOTH is default. So, even you can put that parameter empty.

Change

while (($result = oci_fetch_array($data, OCI_ASSOC)) != false) {

To

while (($result = oci_fetch_array($data, OCI_BOTH)) != false) {

OR To (as OCI_BOTH is default):

while (($result = oci_fetch_array($data)) != false) {

Read it here:

http://php.net/manual/en/function.oci-fetch-array.php

like image 171
Pupil Avatar answered Nov 03 '22 01:11

Pupil