Get array numeric index

Started by Sir Osis of Liver, June 10, 2018, 01:09:39 AM

Previous topic - Next topic

Sir Osis of Liver

For reasons not worth mentioning, I'm creating a manual array and need the highest numeric index in the array -



$link[1] = 'aaaaaaaaaaaaaaaa';
$link[2] = 'bbbbbbbbbbbbbbbb';
$link[4] = 'ccccccccccccc';
$link[5] = 'ddddddddddddddddd';
$link[7] = 'eeeeeeeeeeeeeeee';



In this example, I'm looking for '7'.  Can't use count($link) because there may be gaps in the index, as shown.  Any way to do this?


Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Chen Zhen

You can use end()
ref. http://php.net/manual/en/function.end.php



$link[1] = 'aaaaaaaaaaaaaaaa';
$link[2] = 'bbbbbbbbbbbbbbbb';
$link[4] = 'ccccccccccccc';
$link[5] = 'ddddddddddddddddd';
$link[7] = 'eeeeeeeeeeeeeeee';
end($link);

$lastKey = key($link);


or I suppose you can use array_pop & array_keys


$link[1] = 'aaaaaaaaaaaaaaaa';
$link[2] = 'bbbbbbbbbbbbbbbb';
$link[4] = 'ccccccccccccc';
$link[5] = 'ddddddddddddddddd';
$link[7] = 'eeeeeeeeeeeeeeee';
$lastKey = array_pop(array_keys($link));


.. there are many ways to do it.

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Sir Osis of Liver

That gives you the last value, I need the actual index number - [7].
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Shambles

$keys = array_keys($link);
$last = end($keys);

shawnb61

If you may have numeric and non-numeric keys, I think you'll have to loop thru them & do the test the long way...

Alternatively, you could use array_filter, specify a callback that checks if the key is_int.   Then do a max() on the resulting array.

https://stackoverflow.com/questions/7947940/target-numeric-keys-only-in-array


Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

Chen Zhen

Shambles,
You posted a duplicate of  my first example.


Sir Osis,
Quote from: Sir Osis of Liver
For reasons not worth mentioning, I'm creating a manual array and need the highest numeric index in the array...

The highest numerical index is what you asked for.
Quote
That gives you the last value, I need the actual index number - [7].

That would simply be:

$index7 = $link[7];


Perhaps you are not phrasing your question properly.
Also this post belongs in scripting help.

If you are referring to the value being the highest then use asort() first & then get the last key.
ref. http://php.net/manual/en/function.asort.php

Another alternative is to use arsort() and then use 0 key.

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Chen Zhen


With the array you posted as an example, if you want it to make the keys start at 0 and go in order then you can use array_values()
ref. http://php.net/manual/en/function.array-values.php

ie.

$link[1] = 'aaaaaaaaaaaaaaaa';
$link[2] = 'bbbbbbbbbbbbbbbb';
$link[4] = 'ccccccccccccc';
$link[5] = 'ddddddddddddddddd';
$link[7] = 'eeeeeeeeeeeeeeee';

$links = array_values($link);
print_r($links);


yields:

Array ( [0] => aaaaaaaaaaaaaaaa [1] => bbbbbbbbbbbbbbbb [2] => ccccccccccccc [3] => ddddddddddddddddd [4] => eeeeeeeeeeeeeeee )



My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

Shambles

Quote from: Chen Zhen on June 10, 2018, 10:03:11 AM
Shambles,
You posted a duplicate of  my first example.

A duplicate?

I don't recall using array_pop.

You are not understanding the OPs question. My solution will work for him.

Sir Osis of Liver

Quote from: Shambles on June 10, 2018, 05:05:04 AM
$keys = array_keys($link);
$last = end($keys);


Yep, that's what I wanted.  Now if I can remember what I wanted it for, I'm all set.  Thanks.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Shambles

Lol, can't help you with that  ;D

shawnb61

All of the above solutions work, it just depends on what set of assumptions you're starting with... 

Since the array is 'manually' built, I was concerned you may not get the items inserted into the array in numerical key order.   E.g.:
$link[1] = 'aaaaaaaaaaaaaaaa';
$link[7] = 'eeeeeeeeeeeeeeee';
$link[2] = 'bbbbbbbbbbbbbbbb';
$link[5] = 'ddddddddddddddddd';
$link[4] = 'ccccccccccccc';


If so, many of the methods proposed above would return a value of 4, not 7. 

To solve, just do a max (highest value) instead of an end (last value):
$keys = array_keys($link);
$last = max($keys);


Even that, depending on what is going on, may be making too many assumptions...  For example,  the array might also include non-numeric keys, e.g.:
$link['goof'] = 'zzzzzzzz';
$link[1] = 'aaaaaaaaaaaaaaaa';
$link[7] = 'eeeeeeeeeeeeeeee';
$link[2] = 'bbbbbbbbbbbbbbbb';
$link[5] = 'ddddddddddddddddd';
$link[4] = 'ccccccccccccc';
$link['9ball'] = 'yyyyy';


In this instance, you'll get 9ball, because it sorts highest.

If you need non-numeric keys filtered out, this would work:
$onlynumkeys = array_filter($link, function($k) {return(is_int($k));}, ARRAY_FILTER_USE_KEY);
$lastkey = max(array_keys($onlynumkeys));


(I was on a long drive today, & clearly had too much time to think about this... ::))
Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

Sir Osis of Liver

Quote from: shawnb61 on June 10, 2018, 06:50:01 PM
you may not get the items inserted into the array in numerical key order.

Yeah saw that, max($keys) returns the correct value regardless.  I'm ending up doing it a different way that avoids the problem altogether.  Have to see if it works well for my guy.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Advertisement: