0

I want to select the values from the last key array.

I have this array:

Array
(
    [01] => Array
        (
            [cat_id] => 15
            [offset] => 4951
        )

    [02] => Array
        (
            [cat_id] => 15
            [offset] => 4251
        )

    [03] => Array
        (
            [cat_id] => 15
            [offset] => 4001
        )
)

To get the last key here is my code:

end($completed_steps);
$lastKey = key($completed_steps);

But how can I get the values of the last key? Like, I want to get cat_id and offset.

(sorry for late response, here's an additional note, I had to confirm this first from someone.)

NOTE: I want to store those values as variables. I think I have the idea now.

2
  • find the length of the array and index - 1 with the sum return you last id Commented Oct 2, 2017 at 7:24
  • 1
    what is your expected output ? ["cat_id","offset"] or ["15","4001"] ? or any other output ? Commented Oct 2, 2017 at 7:29

2 Answers 2

2

Result of end() is the last value in array:

$values = end($completed_steps);
print_r($values);
Sign up to request clarification or add additional context in comments.

Comments

-1

Use below code for fetching the values of the last key in array :-

$lastValue = end($completed_steps);
$cat_id = $lastValue['cat_id']);
$offset = $lastValue['offset']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.