Forum Moderators: coopster

Message Too Old, No Replies

Getting the "first" result of a multidimensional array

         

csdude55

2:12 am on Jan 31, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have an array that looks like:

$arr['20200130'][0] = array('2.10', 'foo');
$arr['20200130'][1] = array('2.11', 'bar');
$arr['20200129'][2] = array('2.09', 'lorem');
...
$arr['20190201'][1500] = array('2.14', 'ipsum');


The second dimension will always be in the right order for my purposes, but the first dimension can vary. So how do I fetch the first value when the second dimension is [0], if I don't know what the first dimension is?

I've looked at several sorting operations, but they seem overly complex.

I've also considered this, which I haven't tested but I think would work:

foreach ($arr as $key) {
$top = $arr[$key][0];
break;
}


As a third option, I COULD set a string variable when I built the array, but there are a few times later where I use array_unshift to push a new value to the top under certain conditions. So if I did that then I would have to overwrite that string variable each time.

So is there a better / faster way to get just a single result?

w3dk

9:59 am on Jan 31, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



The second dimension will always be in the right order for my purposes, but the first dimension can vary.


The order of the second dimension would not seem to matter, since you are always getting element "0". Assuming you just want the first element of the first array (the numerical order of the keys is unimportant) then.....

Just think about the first dimension (which you are already with foreach)... an array... and get the first element of that using reset() ...

You don't need to know the "key" of the first element in order to get the first element.


// resets $arr to first element and returns the first value of $arr
$subArray = reset($arr);
$value = $subArray[0];
print_r($value);


Array
(
[0] => 2.10
[1] => foo
)


Which can be simplified to (array dereference directly on the function return value):


$value = reset($arr)[0];


Note that reset() does reset the internal array pointer, but so did foreach() until PHP 7.

csdude55

5:23 am on Feb 1, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's awesome, thanks! I don't think I'd seen the reset() function before, and would have never thought to consider it based on the name.

I actually had to end up going a different direction, though. I realized that I needed to store the key, too, and found that key($arr) would give me the first key. Then, I could easily use that to get the rest:

$key = key($arr);
list($value, $string) = $arr[$key][0];


TIMTOWTDI! Man, I hadn't typed that acronym since last century... LOL

w3dk

10:37 pm on Feb 3, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



I don't think I'd seen the reset() function before, and would have never thought to consider it based on the name.


Yes, reset() is a bit of an unfortunate name (should be "first()" or something?), although that is what it does... it resets the internal array pointer to the first element. The fact that it also returns the value of the first element is almost secondary, although that is probably what it is used most for! Conversely, there is the end() function.

...and found that key($arr) would give me the first key


The key() function actually returns the current key, not necessarily the first key.

To make sure you get the first key, you should still call reset($arr) first - in order to reset the array pointer to the first element.

Alternatively, on PHP 7.3+ there is now array_key_first() - this does not reset the internal array pointer.

TIMTOWTDI!


Absolutely! :)