PHP - Get Numeric Index of Associative Array

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
)

)

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.

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";

Find index of value in associative array in php?

Okay so as I see this problem, you have unique ids, but the names may not be unique.

You could initialize the array as:

array($id=>$name);

And your searches can be like:

array_search($name,$arr);

This will work very well as native method of finding a needle in a haystack will have a better implementation than your own implementation.

e.g.

$id = 2;
$name= 'Sunny';
$arr = array($id=>$name);
echo array_search($name,$arr);

Echoes 2

The major advantage in this method would be code readability.

find numeric key of associative index of array in array

As mentioned, there is no numeric index for that element, as its key is a string.

However if you just want to find its position in the array:

$position = array_search("Index1",array_keys($arr),true);

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];

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 echo certain position of associative array?

Use array_keys() to get the keys into an array. Then access the 2D array using indexes in the keys array. Not that this is the best way to do this but it is a way to use numeric indexes to solve your problem.

$keys = array_keys($_SESSION["myarr"]);
$zero = $_SESSION["myarr"][$keys[0]];


Related Topics



Leave a reply



Submit