简体   繁体   English

如何在php的多维数组中找到最后一个值?

[英]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. 我正在使用多维数组,并且尝试使用php查找其中一个数组的最后一个值,并根据该最后一个数字(值)向其添加另一个。

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? 我正在尝试创建此结构,但现在要查找的是add_complete数组中的最后一个数字,而不是现在说1的位置,然后再添加一个数字并将其替换为1。我做那个吗?

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

If you're assigning a value you can also just remove the 1 : 如果您要分配值,则也可以删除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: ...但是有些人对此感到不满意,我觉得它在将来的PHP版本中将不受支持,因此请坚持Justin Ethier的回答:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM