User login

Find the next largest key in an array

php find the next largest key in an array
php find the next greatest key in an array

There is undoubtedly a better way, but recursion has melted my brain and search engines aren't saving me.

So this is what I did... actually, I used another approach entirely that didn't need this. But array_keys is how I would have started, then sorted it, and then had a foreach that compared my key to the keys it got as it walked through, and broken out of there with the new key the first one that was larger.

    $terms_on_this_level = array_keys($parents);

I know there would have been a better way, but it's moot to me know anyway... until I search for this answer again and find my own, deeply unfulfilling, page.

Agaric Design: Existentialism meets web design.

UPDATE: Agaric did try using this method, and it foundered on the strangest thing. The PHP sort() function didn't sort or function.

$t = array("apple", "orange", "banana");
agaric_a($t);
    $new = sort($t);
drupal_set_message($new);
agaric_a($new);

And the output:

  • Array
    (
    [0] => apple
    [1] => orange
    [2] => banana
    )

  • 1
  • 1

For the love of Frankenstein, the darned thing only does its work by reference. It doesn't return a blessed thing. That's why sort didn't work.

Writing these, uh, tutorials sure help me. I feel sorry for anyone who reads them though.

Revised code:

$t = array("apple", "orange", "banana");
agaric_a($t);
sort($t);
drupal_set_message($t);
agaric_a($t);

And the result now:

<

blockquote>

  • Array
    (
    [0] => apple
    [1] => orange
    [2] => banana
    )
  • Array
  • Array
    (
    [0] => apple
    [1] => banana
    [2] => orange
    )

So the moral of the story is that PHP's sort function acts on the array passed it by reference, and returns nothing useful.

We do have a function for you out of all this:

/**
* Get the next highest key in an array
*
* An extremely unoptimized function within an extremely unoptimized function.
*/
function _cmt_next_highest_key($array, $key = 0) {
  $array_keys = array_keys($array);
  sort($array_keys);
  foreach ($array AS $array_key) {
    if ($array_key > $key)  return $array_key;
  }
  // there was no larger key
  return FALSE;
}

php find the next largest key in an array
php find the next greatest key in an array

There is undoubtedly a better way, but recursion has melted my brain and search engines aren't saving me.

So this is what I did... actually, I used another approach entirely that didn't need this. But array_keys is how I would have started, then sorted it, and then had a foreach that compared my key to the keys it got as it walked through, and broken out of there with the new key the first one that was larger.

    $terms_on_this_level = array_keys($parents);

I know there would have been a better way, but it's moot to me know anyway... until I search for this answer again and find my own, deeply unfulfilling, page.

Agaric Design: Existentialism meets web design.

UPDATE: Agaric did try using this method, and it foundered on the strangest thing. The PHP sort() function didn't sort or function.

$t = array("apple", "orange", "banana");
agaric_a($t);
    $new = sort($t);
drupal_set_message($new);
agaric_a($new);

And the output:

  • Array
    (
    [0] => apple
    [1] => orange
    [2] => banana
    )

  • 1
  • 1

For the love of Frankenstein, the darned thing only does its work by reference. It doesn't return a blessed thing. That's why sort didn't work.

Writing these, uh, tutorials sure help me. I feel sorry for anyone who reads them though.

Revised code:

$t = array("apple", "orange", "banana");
agaric_a($t);
sort($t);
drupal_set_message($t);
agaric_a($t);

And the result now:

<

blockquote>

  • Array
    (
    [0] => apple
    [1] => orange
    [2] => banana
    )
  • Array
  • Array
    (
    [0] => apple
    [1] => banana
    [2] => orange
    )

So the moral of the story is that PHP's sort function acts on the array passed it by reference, and returns nothing useful.

We do have a function for you out of all this:

/**
* Get the next highest key in an array
*
* An extremely unoptimized function within an extremely unoptimized function.
*/
function _cmt_next_highest_key($array, $key = 0) {
  $array_keys = array_keys($array);
  sort($array_keys);
  foreach ($array AS $array_key) {
    if ($array_key > $key)  return $array_key;
  }
  // there was no larger key
  return FALSE;
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.