我在StackOverflow上发现了一些类似的问题,但我的问题不同。我会尽量解释得更清楚。首先是阵列结构:$appointment
Array (
[id_users_provider] => 85
[start_datetime] => 2015-11-15 17:15:00
[end_datetime] => 2015-11-15 17:15:00
[notes] =>
[is_unavailable] =>
[id_users_customer] => 87
[id_services] => 15
)
Array (
[id_users_provider] => 85
[start_datetime] => 2015-11-15 17:15:00
[end_datetime] => 2015-11-15 17:15:00
[notes] =>
[is_unavailable] =>
[id_users_customer] => 87
[id_services] => 13
)
如何查看$appointment
变量中包含的两个数组。现在我想要得到最后一个数组的末尾,在本例中是id_services:13的数组。实际上,我通过appointment['id_services']
执行了一次迭代。像这样:
foreach($appointment['id_services'] as $services)
{
print_r(end($appointment));
}
但这个给我回了:
15
13
这是错误的,因为在这种情况下我只想得到13。我该怎么做?主要问题是在foreach循环内部检查实际的$services
是否是foreach循环内最后一个数组的最后一个键。
伙计,为什么不只是end($appointment)['id_services']
?在这种情况下,您为什么需要foreach
?
$last_appointment = end($appointment);
$id_services = $last_appointment['id_services'];
$id_services === 13; // true
foreach循环错误。。。。
两种简单的方法…
// using count (not recommended)
echo $appointment[count($appointment)-1]['id_services'];
//using end
echo end($appointment)['id_services'];
根据你的评论,你可能正在尝试这样做(我不明白为什么)
$last_appointment = end($appointment);
echo end($last_appointment);
修复您的代码
//not recommended!!
foreach(end($appointment) as $services)
{
print_r(end($services));
}