0

Althoug this works well enough, I am curious if anyone knows of a prettier way of doing this as this situation seems to come up quite often.

<?php
//Initialy,  data is nested up in $some_array[0] ...
$some_array = array(array('somevar' => "someValue", "someOtherVar" => "someOtherValue"));

print_r($some_array);   

Array ( [0] => Array ( [somevar] => someValue [someOtherVar] => someOtherValue ) )

// Could the following line be achieved a more elegant fashion?
$some_array = $some_array[0];

print_r($some_array);

// Prints the intended result:  

Array ( [somevar] => someValue [someOtherVar] => someOtherValue )

Does anyone know of a way to achieve this with a native function or in a more elegant fashion?

Thanks!

hakre
  • 193,403
  • 52
  • 435
  • 836
Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • what's wrong with using `$some_array = $some_array[0];`? It seems fine to me... – Jocelyn Jul 31 '12 at 01:25
  • Please define "more elegant". Also why is your data not more elegant in the first place if you strive for elegance that much? The native function is called [`reset`](http://php.net/reset) btw. – hakre Jul 31 '12 at 05:46
  • @hakre - More elegant was having a native function that returns first element of array, like current() seems to do. On re-thhinking on this a bit I would tend to favor my initial code example for readability, as one can see with half-a-glance and without thinking what is going on. Data is structured in this manner because it is the return value of a callback from a framework. PS: reset needs to be used in conjunction with current(). In my case reset is somewhat redundant as I could call current() right after the function call. – Stephane Gosselin Jul 31 '12 at 15:56
  • @stefgosselin: You don't need to use `current` with `reset`, I added an answer for explicit clarification with not only some example code, but also some linked demonstration. Hope it helps ;) – hakre Jul 31 '12 at 17:23

2 Answers2

2

The native function you're looking for is called reset (Demo):

$some_array = reset($some_array);

For explicit clarification: current is not necessary.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • does not work, if array contains more, than 1 element on the upper level: reset([[1, 2, 3], [4, 5, 6]]) -> [1, 2, 3] http://codepad.org/jqS2Tu23 – Lajos Mészáros Jan 28 '21 at 17:21
  • @LajosMeszaros: That is one element: array. Sounds like you're looking for something different, like to flatten the array first: https://stackoverflow.com/q/1319903/367456 – hakre Jan 28 '21 at 17:44
  • The solution I ended up using is calling array_reduce on the input array with array_merge inside. – Lajos Mészáros Jan 28 '21 at 17:54
  • @LajosMeszaros: Or you may want to consider anything iterable moving empty elements out of the way, too: https://3v4l.org/1BK82 – hakre Jan 28 '21 at 18:40
1

You could use current (explained here), it basically points to the first element in the array and returns it.

To be absolutely sure you get the first element, you should reset your array, like so:

reset($arr)
$firstElement = current($arr)
Mekswoll
  • 1,355
  • 3
  • 15
  • 35
  • 2
    [`reset`](http://php.net/reset) returns as well, so `$first = reset($array);` should be enough. – hakre Jul 31 '12 at 05:47
  • for a true (non-associative) array `reset` would suffice. But in the php.net documentation it mentions in the user comments that this does not work for associative arrays, which is why I added the `current` call. – Mekswoll Jul 31 '12 at 18:31