5

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
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Murali krishna
  • 823
  • 1
  • 8
  • 23
  • http://stackoverflow.com/questions/4769148/accessing-an-associative-array-by-integer-index-in-php Take a look at this. – Matt Dec 23 '15 at 06:16

3 Answers3

7

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

Pupil
  • 23,834
  • 6
  • 44
  • 66
4

You can try this -

$result= array_values($result); // Array with indexes you need

Or you can do another trick (assuming you are having those indexes dynamically) -

$keys = array(
    0 => 'USER_ID',
    1 => 'USER_DESCRIPTION',
);

while (($result = oci_fetch_array($data, OCI_ASSOC)) != false) {
    echo $result[$keys[0]]. ' - ' .$result[$keys[1]];
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
3

This works for me.

while (($result = oci_fetch_array($data, OCI_NUM)) != false){}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Murali krishna
  • 823
  • 1
  • 8
  • 23