[Solved] How can I get the key of the last element in an array?


In Custom Scripting, I want to get the key of the last element in an array.

The sgapiArray_Pop method only returns the final value, but I want the key/value pair.

Ideally I would like to use PHP’s array_slice method, like in http://stackoverflow.com/questions/6933403/array-pop-with-key.

Can this method be made available or is there another way I can access this value? My keys are not numeric, nor is the order known previously, so I cannot use sgapiCount to infer the value.


Thanks for any tips!

kristypjames asked

    Best answer

    qc 560 Rep.

    Try using sgapiArray_Flip() to swap the keys and values, then followed by using sgapiArray_Pop() on the flipped array to get the last value (which should be the key from the original array).

    qc answered

      Note that I also cannot use sgapiArrayGet(%%array, %%index/key), because this can only access the array’s value with the key when it is known.

      kristypjames answered


        Question stats

        • Active
        • Views5869 times
        • Answers2 answers
        • Followers0 followers