How to access an array in php by index?

I have a database table for the user, I have two fields USER_ID and USER_DESCRIPTION. If I run the following code, I get an array in the form.

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

But I want to access this value in an index based on 0, 1. As I understand it.

 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 } 
+5
source share
3 answers

You passed the second parameter OCI_ASSOC to oci_fetch_array() , which will only retrieve the associative array.

If you change this parameter to OCI_BOTH , it will return both a numeric and associative array.

OCI_BOTH - the default value. Thus, even you can leave this parameter empty.

Change

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

For

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

OR IN (as OCI_BOTH by default):

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

Read here:

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

+7
source

You can try this -

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

Or you can do one more trick (assuming you have these 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]]; } 
+4
source

This works for me.

 while (($result = oci_fetch_array($data, OCI_NUM)) != false){} 
+3
source

Source: https://habr.com/ru/post/1239011/


All Articles