PHP - Get Array Value with a Numeric Index

Using php extract function with numeric indexes

As per the manual,

You must use an associative array; a numerically indexed array will not produce results unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID.

You can add a prefix to your 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_values

array_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:

    $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.

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

Consider which of these behaviours you actually need. (It may be that either will do for your purposes.)

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



Leave a reply



Submit