Using php extract function with numeric indexes
As per the manual,
You can add a prefix to yourYou must use an associative array; a numerically indexed array will not produce results unless you use
EXTR_PREFIX_ALL
orEXTR_PREFIX_INVALID
.
extract()
function. This would, in the example below, add the var_
prefix to each instance. You can supply whatever valid variable-prefix as the third argument - the created variables would reflect that parameter in the extract()
function. $my_array = array(0 =>"Cat", 1=>"Dog", 2=>"Horse");
extract($my_array, EXTR_PREFIX_ALL, "var");
The results can now be found in $var_0
through $var_2
.- Documentation
- Live demo
PHP - Get Array value by Index instead of Key
Try:
foreach ($array as $key => $value) {
echo "Key: $key";
}
Where $array is the array you want to loop through.It will print out all the keys
php - get numeric index of associative array
echo array_search("car",array_keys($a));
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
)
)
Getting a value from a php array using the index number
Re-index so you can use a numeric index:
echo array_values($alldata[0][6][0])[0];
Or for them all:$result = array_values($alldata[0][6][0]);
echo $result[0];
echo $result[2];
Can't we use numeric index on arrays with key value in PHP?
No you can't access a value of an associative array via numerical index. But you can pass your array through array_values
first to get what you want:
echo array_values($myArray)[1];
http://php.net/array_valuesarray_values() returns all the values from the array and indexes the array numerically.
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:
But the value "one" is at$newArray = array_keys($array);
echo $newArray[0];$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);
[1]
doesn't correspond to "one" any which way you turn it. How to check if PHP array is associative or sequential?
You have asked two questions that are not quite equivalent:
- Firstly, how to determine whether an array has only numeric keys
- Secondly, how to determine whether an array has sequential numeric keys, starting from 0
The first question (simply checking that all keys are numeric) is answered well by Captain kurO.
For the second question (checking whether the array is zero-indexed and sequential), you can use the following function:
function isAssoc(array $arr)
{
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
var_dump(isAssoc(['a', 'b', 'c'])); // false
var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false
var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true
var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true
Related Topics
MySQL and Query to Satisfy on Same Column
When Should I Close a Database Connection in PHP
What Is the Correct Format for a Blowfish Salt Using PHP's Crypt
Laravel Catch Eloquent "Unique" Field Error
Php: How to See "Invisible" Characters Like \N
How to Make a Chat Room Script with PHP
Object Copy Versus Clone in PHP
Get Query Back from Pdo Prepared Statement
How to Use PHPize After Update to MACos Mojave
Exploding by Array of Delimiters
How to Access Outer Local Variable in PHP
How to Refresh Select2 Dropdown Menu After Ajax Loading Different Content
Config Nginx for Laravel in a Subfolder