Php 获取字符串/数组的最后一个值

Php 获取字符串/数组的最后一个值,php,arrays,string,Php,Arrays,String,我使用了以下PHP代码,它给出了以下输出: $datetimearray = explode(" ", $mydatetime); $last = pos($datetimearray); var_dump($last); 输出: string10 2014-06-07 string10 2014-06-08 string10 2014-06-09 我不知道这意味着什么。它不是数组,也不是单个字符串。 如何获取该值的最后一个值2014-06-09 更新 下面是CakePHP的完整代码。我忘了我

我使用了以下PHP代码,它给出了以下输出:

$datetimearray = explode(" ", $mydatetime);
$last = pos($datetimearray);
var_dump($last);
输出: string10 2014-06-07 string10 2014-06-08 string10 2014-06-09

我不知道这意味着什么。它不是数组,也不是单个字符串。 如何获取该值的最后一个值2014-06-09

更新 下面是CakePHP的完整代码。我忘了我在循环中

foreach ($valuations as $valuation):
  $mydatetime = $valuation['Valuation']['timestamp'];
  $datetimearray = explode(" ", $mydatetime);
  $last = pos($datetimearray);
  var_dump($last);
endforeach;

当然,你不会在循环中这样做吗?pos是current的别名,因此每次调用都返回数组的当前元素


要获取最后一个元素,请查看end

有一种更简单的方法,使用count减1来获取数组中的最后一项:

$datetimearray = explode(" ", $mydatetime);
$last = $datetimearray[count($datetimearray) - 1)];
var_dump($last);

假设估值已按日期排序:

$last_valuation = end($valuations); // inspect last element of array
$last_date = strtok($last_valuation['Valuation']['timestamp'], ' ');

另请参见:

您根本不需要分解和数组:substrstrrchr$mydatetime',1请发布整个代码,因为这显然是在某种循环中运行的。您是对的,对不起,我在循环中。我更新了完整代码