0

hi i have a multidimensional l array .

Array
(
    [1] => Array
        (
            [38] => Fashion Retail | Fashion Accessories
        )

    [10] => Array
        (
            [194] => Automotive | 4x4
            [206] => Automotive | Aftermarket Parts and Kits
            [201] => Automotive | ATVs
        )

)

i want to get the first sub array's key , in this scenario it is 1 , i can get it using a foreach loop .

foreach($myarry as $key=>$val)

is there any way to achieve this with out looping , please help . thanks in advance

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

5 Answers5

1

If using >= PHP 5.5...

$first = array_keys($myarry)[0];

If using an older PHP, just assign the keys somewhere, and then subscript the first element as normal.

alex
  • 479,566
  • 201
  • 878
  • 984
1
$arrKeys = array_keys($array);
$key = array_shift($arrKeys); // gives first key

Returns the first key and deletes this from the arrKeys, hence the next key, 10 in this case will be returned on next call. No need to make another array.

web-nomad
  • 6,003
  • 3
  • 34
  • 49
1

yep, I did that , I have used

current(array_keys($my_array))
bla
  • 25,846
  • 10
  • 70
  • 101
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0
print_r($myarry[array_keys($myarry)[0]]);
Varun Bajaj
  • 1,033
  • 8
  • 16
0

Please try:

reset($myarry);
$first_key = key($myarry);
Sankar V
  • 4,110
  • 5
  • 28
  • 52