Get the first key in a multidimensional array using PHP

I would like to get the first key from this multidimensional array.

Array ( [User] => Array ( [id] => 2 [firstname] => first [lastname] => last [phone] => 123-1456 [email] => [website] => [group_id] => 1 [company_id] => 1 ) ) 

This array is stored in $ this-> data.

Right now I am using a key ($ this-> data) that retrieves the "User" as it should, but this does not seem to be the correct way to achieve the result.

Are there any other ways to get this result?

thanks

+6
arrays php multidimensional-array
source share
3 answers

There are other ways to do this, but nothing is as quick and short as using key() . Each other use is for all keys. For example, all of them will return the first key in the array:

 $keys=array_keys($this->data); echo $keys[0]; //prints first key foreach ($this->data as $key => $value) { echo $key; break; } 

As you can see, both of them are sloppy.

If you want to use oneliner, but you want to protect yourself from accidentally receiving the wrong key, if the iterator is not on the first element, try the following:

 reset($this->data); 

reset ():

reset () rewinds the internal array pointer to the first element and returns the value of the first array element.

But what you do looks great to me. There is a function that does exactly what you want on a single line; what else can you want?

+8
source share

Use this (PHP 5.5+):

 echo reset(array_column($this->data, 'id')); 
0
source share

I had a similar problem and was glad to find this post. However, the solutions presented only work for 2 levels and do not work for a multi-dimensional array with any number of levels. I need a solution that could work for an array with any dimension and find the first keys of each level.

After a bit of work, I found a solution that might be useful for someone else, and so I included my solution in this post.

Here is an example starting array:

  $myArray = array( 'referrer' => array( 'week' => array( '201901' => array( 'Internal' => array( 'page' => array( 'number' => 201, 'visits' => 5 ) ), 'External' => array( 'page' => array( 'number' => 121, 'visits' => 1 ) ), ), '201902' => array( 'Social' => array( 'page' => array( 'number' => 921, 'visits' => 100 ) ), 'External' => array( 'page' => array( 'number' => 88, 'visits' => 4 ) ), ) ) ) ); 

Since this function should display all the first keys regardless of the size of the array, this assumes a recursive function, and my function looks like this:

 function getFirstKeys($arr){ $keys = ''; reset($arr); $key = key($arr); $arr1 = $arr[$key]; if (is_array($arr1)){ $keys .= $key . '|'. getFirstKeys($arr1); } else { $keys = $key; } return $keys; } 

When a function is called using code:

 $xx = getFirstKeys($myArray); echo '<h4>Get First Keys</h4>'; echo '<li>The keys are: '.$xx.'</li>'; 

exit:

Get the first keys

  • Keys: Referrer | week | 201901 | internal | page | number

I hope this saves someone some time if they run into a similar problem.

0
source share

All Articles