zeckdude
zeckdude

Reputation: 16173

How can I find the last value in a multidimensional array in php?

I am using a multidimensional array and I am trying to use php to find the last value of one of the arrays and based on that last number(value), add one more to it.

Here is my multidimensional array structure:

$_SESSION['cart']['add_complete'][1]['deck_id']

I am trying to create this structure, but instead of where it says 1 now, I want to find out what the last number in the add_complete array is and then add one more to it and put that in the place of the 1. How can I do that?

Upvotes: 0

Views: 165

Answers (2)

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

If you're assigning a value you can also just remove the 1:

$_SESSION['cart']['add_complete'][]['deck_id'] = 'wtv';

... but this is frowned upon some, and I am under the impression it won't be supported in future versions of PHP, so stick with Justin Ethier's answer:

$_SESSION['cart']['add_complete'][count( $_SESSION['cart']['add_complete'] ) + 1]['deck_id']

Upvotes: 1

Justin Ethier
Justin Ethier

Reputation: 134227

$new_num = count( $_SESSION['cart']['add_complete'] ) + 1;

Upvotes: 1

Related Questions