Accessing an associative array by integer index in PHP
Use array_keys.
$keys = array_keys($my_arr);
$my_arr[$keys[1]] = "not so much bling";
How to access an to a value associative array using index position in PHP
Not if you want to keep it as an associative array. If you want to use numeric key indexes you could do this:
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
$fruits2 = array_values($fruits);
echo $fruits2[2];
Find out more about array_values()
at the PHP manual.
UPDATE: to compare two associative arrays as you mentioned in your comment you can do this (if they have the same keys -- if not you should add isset()
checks):
foreach (array_keys($arr1) as $key) {
if ($arr1[$key] == $arr2[$key]) {
echo '$arr1 and $arr2 have the same value for ' . $key;
}
}
Or, to avoid the array_keys function call:
foreach ($arr1 as $key => $val) {
if ($val == $arr2[$key]) {
echo '$arr1 and $arr2 have the same value for ' . $key;
}
}
How does PHP index associative arrays?
Within your user defined array you are assigning the keys manually your array means as
array(1 => 'One',3, 2 => 'Two');//[1] => One [2] => 3 [2] => Two
Here we have two identical index and as per DOCS its mentioned that the last overwrite the first
Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1.
Note that when two identical index are defined, the last overwrite the first.
In order to filter this case you can simply make some changes as
array(1 => 'One',2 =>'Two',3) // array ([1] => One [2] => Two [3] => 3)
array(3,1 => 'One',2 =>'Two') //array ([0] => 3 [1] => One [2] => Two)
array(1 => 'One',2 => 3 ,3 =>'Two')// array([1] => One [2] => 3 [3] => Two)
DOCS CHECK PARAMETERS
php: how to get associative array key from numeric index?
You don't. Your array doesn't have a key [1]
. You could:
Make a new array, which contains the keys:
$newArray = array_keys($array);
echo $newArray[0];But the value "one" is at
$newArray[0]
, not[1]
.
A shortcut would be:echo current(array_keys($array));
Get the first key of the array:
reset($array);
echo key($array);Get the key corresponding to the value "value":
echo array_search('value', $array);
This all depends on what it is exactly you want to do. The fact is, [1]
doesn't correspond to "one" any which way you turn it.
get numeric index of associatve array without specifying the key name
To get numeric index of associative array without specifying the key name you need to use the array_values()
function.
Example:
$arr = [
'oneName' => [
'val' => 'str',
'price' => 'int'
],
'twoName' => [
'val' => 'str',
'price' => 'int'
]
];
$arr = array_values($arr);
print_r($arr);
Output:
Array
(
[0] => Array
(
[val] => str
[price] => int
)
[1] => Array
(
[val] => str
[price] => int
)
)
How to access array in php by index based?
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
php - get numeric index of associative array
echo array_search("car",array_keys($a));
Get key & value from associative array using numeric index
Use array_keys
to get the keys:
$keys = array_keys($array);
$key = $keys[0];
$value = $array[$key];
Related Topics
Understanding Imagemagick's Convert and Translating to Ruby Rmagick
What Does the B in Front of String Literals Do
Display Output in Parts in PHP
Use Openssl_Encrypt to Replace Mcrypt for 3Des-Ecb Encryption
Sum Specific Values in a Multidimensional Array (Php)
Composer Require Local Package
Simple PHP Form: Attachment to Email (Code Golf)
How to Run a Cronjob Every X Minutes
How to Use Break or Continue Within for Loop in Twig Template
How to Paginate a Merged Collection in Laravel 5
Run a PHP Script Every Second Using Cli
PHP Get Both Array Value and Array Key
Remotely Destroy a Session in PHP (User Logs in Somewhere Else)
How to Manage a Single PHP5 Session on Multiple Apache Servers
How to Create an Empty Array in PHP with Predefined Size