Php 如何在for循环中创建数组并在for循环外访问该数组
我试图将值作为数组存储在Php 如何在for循环中创建数组并在for循环外访问该数组,php,arrays,laravel,Php,Arrays,Laravel,我试图将值作为数组存储在for循环中,以便在循环外部访问它们 foreach ($doctor->booking as $booking) { $bookeddate = date('Y-m-d', strtotime($booking->booked_time)); if( $bookeddate == $end_date ) { $booked_time[] = date('H:i a', strtotime($booking->booke
for
循环中,以便在循环外部访问它们
foreach ($doctor->booking as $booking) {
$bookeddate = date('Y-m-d', strtotime($booking->booked_time));
if( $bookeddate == $end_date ) {
$booked_time[] = date('H:i a', strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
此代码不断给我一个错误“未定义变量:预定时间”
请重试
在使用之前初始化$booked\u time
,因为它仅在函数内部具有作用域
$booked_time = [];
foreach ($doctor->booking as $booking) {
$bookeddate=date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
$booked_time[]=date('H:i a',strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
试一试
在使用之前初始化$booked\u time
,因为它仅在函数内部具有作用域
$booked_time = [];
foreach ($doctor->booking as $booking) {
$bookeddate=date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
$booked_time[]=date('H:i a',strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
这是不可能的,因为块范围的概念。您可以阅读有关PHP中变量作用域的更多信息 通常,这意味着在花括号内声明的每个变量只能在这段代码中访问 解决此问题的最简单方法是事先创建一个空数组,如下所示:
$booked_time = [];
foreach ($doctor->booking as $booking) {
$bookeddate=date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
$booked_time[]=date('H:i a',strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
这是不可能的,因为块范围的概念。您可以阅读有关PHP中变量作用域的更多信息 通常,这意味着在花括号内声明的每个变量只能在这段代码中访问 解决此问题的最简单方法是事先创建一个空数组,如下所示:
$booked_time = [];
foreach ($doctor->booking as $booking) {
$bookeddate=date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
$booked_time[]=date('H:i a',strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
在for循环外部创建数组 然后使用
array\u push()
将元素添加到数组中
可能的解决办法
$booked_times = [];
foreach ($doctor->booking as $booking) {
$bookeddate = date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
array_push($booked_times, date('H:i a',strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
在for循环外部创建数组 然后使用
array\u push()
将元素添加到数组中
可能的解决办法
$booked_times = [];
foreach ($doctor->booking as $booking) {
$bookeddate = date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
array_push($booked_times, date('H:i a',strtotime($booking->booked_time));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
这是不适用的 您创建的数组仅对该作用域有效(此处为FOREACh) 一旦从该作用域中退出,数组变量就会显示出来。 溶液-
在全局作用域中声明您的数组,其中两个foreach都可以访问。这不适用 您创建的数组仅对该作用域有效(此处为FOREACh) 一旦从该作用域中退出,数组变量就会显示出来。 溶液-
在全局范围内声明您的数组,其中两个foreach都可以访问。您应该做三件事:
$booked_time=[]代码>-这将使错误消失
你应该做三件事:
$booked_time=[]代码>-这将使错误消失
<>你可能想考虑使用一个集合。他们真的很酷,你可以使用很多方法。 我将使用上面您喜欢的文章中的代码,将数组更改为集合
$booked_time = collect();
foreach ($doctor->booking as $booking) {
$bookeddate=date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
$booked_time->push(date('H:i a',strtotime($booking->booked_time)));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
<>你可能想考虑使用一个集合。他们真的很酷,你可以使用很多方法。 我将使用上面您喜欢的文章中的代码,将数组更改为集合
$booked_time = collect();
foreach ($doctor->booking as $booking) {
$bookeddate=date('Y-m-d',strtotime($booking->booked_time));
if($bookeddate==$end_date){
$booked_time->push(date('H:i a',strtotime($booking->booked_time)));
}
}
foreach ($booked_time as $key ) {
echo $key;
}
可能是因为您从未通过此测试
if($bookeddate==$end\u-date){
。如果要避免这种情况,请确保将var初始化为空数组,或者使用isset
进行测试,也可能是因为从未满足if语句条件而从未创建$bookeddate\u-time?if($bookeddate=$end\u-date)如果此条件不成立,则$bookeddate==$end\u date可能不确定,因为您从未通过此测试if($bookeddate==$end\u date){
。如果要避免这种情况,请确保将var初始化为空数组,或者使用isset
进行测试,也可能是因为从未满足if语句条件而从未创建$booked\u time?if($bookeddate=$end\u date)如果这个条件不成立,那么$booked_time将是未定义的谢谢你。Iwrong在哪里-?非常感谢你这么做。Iwrong在哪里-?我错在哪里-?为了访问你的变量‘$a’,你首先需要初始化它。这必须发生,否则你的变量没有定义,并且在你的ca中se,解释器不会知道它是一个数组。我错在哪里-?为了访问变量‘$a’,你首先需要初始化它。这必须发生,因为否则你的变量没有定义,在你的情况下,解释器不会知道它是一个数组。PHP的作用域不是这样工作的。Declar在for循环中声明变量不会阻止以后使用它。PHP的作用域不会以这种方式工作。在for循环中声明变量不会阻止以后使用它。