Looking for a shortcut: Knowing the index position of an array, howcan i get the next $key available

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SM

    #1

    Looking for a shortcut: Knowing the index position of an array, howcan i get the next $key available

    Hello,

    Knowing the index position of an array, how can i get the next $key
    available, if it exists. I've found a solution but I was wondering if
    there was a shorcut in PHP.

    i.e:

    $cats = array (
    456=>'myown',
    186=>'getto',
    714=>'many',
    484=>'wondering ',
    775=>'ok'
    );

    In this example, I know that the index postion is 2. So the return
    $key should be the next one available, if it exists... that is: 484.
    This is how i coded:

    $index = 2;
    $id = array_slice($ca ts, $index, 1, true);

    foreach($id as $key=>$value) {
    echo $key;
    }

    I've look at all the functions on the php web site and couldn't find a
    shortcut to this.

    Any ideas?

    Thanks in advance
    Marco
  • macca

    #2
    Re: Looking for a shortcut: Knowing the index position of an array,how can i get the next $key available

    Advance the internal pointer of an array

    Comment

    • Leigh Finch

      #3
      Re: Looking for a shortcut: Knowing the index position of an array,how can i get the next $key available

      SM wrote:
      Hello,
      >
      Knowing the index position of an array, how can i get the next $key
      available, if it exists. I've found a solution but I was wondering if
      there was a shorcut in PHP.
      >
      i.e:
      >
      $cats = array (
      456=>'myown',
      186=>'getto',
      714=>'many',
      484=>'wondering ',
      775=>'ok'
      );
      >
      In this example, I know that the index postion is 2. So the return
      $key should be the next one available, if it exists... that is: 484.
      This is how i coded:
      >
      $index = 2;
      $id = array_slice($ca ts, $index, 1, true);
      >
      foreach($id as $key=>$value) {
      echo $key;
      }
      >
      I've look at all the functions on the php web site and couldn't find a
      shortcut to this.
      >
      Any ideas?
      >
      Thanks in advance
      Marco
      Hi Marco,
      I think I understand what you are trying to do. Why not increase index
      by 1 in the array slice function?

      A quicker/nicer way to return the current key of an array, is to use the
      function key(), rather than using a foreach loop.

      Heres what I came up with

      $index = 2; //known index

      $id = array_slice($ca ts, $index + 1, 1, true); // splice, the next index

      echo key($id); echo the key

      Alternatively, you could condense it down a line:

      $index = 2; //known index

      echo key(array_slice ($cats, $index + 1, 1, true)); // echo out the next
      index

      Hope this helps, Cheers
      Leigh Finch

      Comment

      • Mikhail Kovalev

        #4
        Re: Looking for a shortcut: Knowing the index position of an array,how can i get the next $key available

        On 25 Mai, 07:54, SM <servandomont.. .@gmail.comwrot e:
        Hello,
        >
        Knowing the index position of an array, how can i get the next $key
        available, if it exists. I've found a solution but I was wondering if
        there was a shorcut in PHP.
        >
        i.e:
        >
        $cats = array (
                456=>'myown',
                186=>'getto',
                714=>'many',
                484=>'wondering ',
                775=>'ok'
        );
        >
        In this example, I know that the index postion is 2. So the return
        $key should be the next one available, if it exists... that is: 484.
        This is how i coded:
        >
        $index = 2;
        $id = array_slice($ca ts, $index, 1, true);
        >
        foreach($id as $key=>$value) {
                echo $key;
        >
        }
        >
        I've look at all the functions on the php web site and couldn't find a
        shortcut to this.
        >
        Any ideas?
        >
        Thanks in advance
        Marco
        $cats_keys = array_keys($cat s);

        echo $cats_keys[3];

        ?

        Comment

        • SM

          #5
          Re: Looking for a shortcut: Knowing the index position of an array,how can i get the next $key available

          On May 25, 6:32 am, Mikhail Kovalev <mikhail_kova.. .@mail.ruwrote:
          On 25 Mai, 07:54, SM <servandomont.. .@gmail.comwrot e:
          >
          >
          >
          Hello,
          >
          Knowing the index position of an array, how can i get the next $key
          available, if it exists. I've found a solution but I was wondering if
          there was a shorcut in PHP.
          >
          i.e:
          >
          $cats = array (
          456=>'myown',
          186=>'getto',
          714=>'many',
          484=>'wondering ',
          775=>'ok'
          );
          >
          In this example, I know that the index postion is 2. So the return
          $key should be the next one available, if it exists... that is: 484.
          This is how i coded:
          >
          $index = 2;
          $id = array_slice($ca ts, $index, 1, true);
          >
          foreach($id as $key=>$value) {
          echo $key;
          >
          }
          >
          I've look at all the functions on the php web site and couldn't find a
          shortcut to this.
          >
          Any ideas?
          >
          Thanks in advance
          Marco
          >
          $cats_keys = array_keys($cat s);
          >
          echo $cats_keys[3];
          >
          ?
          Thanks guys. I've tried a combination of all the solutions proposed
          and it work perfectly!

          Marco

          Comment

          Working...