0

我有以下代码,但我不确定为什么我的数组仍然是空的,因为我的想法是如果 foreach 是空的,它应该加载带有“No Open Homes”的 else 语句:

$openHomeTimes = array();

                    if(!empty($detail) && in_array('OpenHomes', $detail))
                    {
                        foreach ($detail['OpenHomes'] as $openHome) 
                        {   
                            $startO        = $openHome['Start'];
                            $finishO       = $openHome['End'];
                            $startConvert  = preg_replace('~\D~', '', $startO);
                            $start         = date('D j M g a',$startConvert / 1000);
                            $finishConvert = preg_replace('~\D~', '', $finishO);
                            $finish        = date('g:ia',$finishConvert / 1000);

                            $openHomeDetail = $start." - ". $finish;

                            $openHomeTimes[] = $openHomeDetail;


                        }
                    }else{
                        if(!empty($openHomeTimes))
                        {
                            $message = 'No Open Homes';

                            $openHomeTimes = $message;


                        }
                    }
4

1 回答 1

0

如果我没有完全误解你,简单的逻辑修复可能就足够了。该 foreach 不是外部 if/else 构造的条件。

                $openHomeTimes = array();

                if(!empty($detail) && in_array('OpenHomes', $detail))
                {
                    foreach ($detail['OpenHomes'] as $openHome) 
                    {   
                        $startO        = $openHome['Start'];
                        $finishO       = $openHome['End'];
                        $startConvert  = preg_replace('~\D~', '', $startO);
                        $start         = date('D j M g a',$startConvert / 1000);
                        $finishConvert = preg_replace('~\D~', '', $finishO);
                        $finish        = date('g:ia',$finishConvert / 1000);

                        $openHomeDetail = $start." - ". $finish;

                        $openHomeTimes[] = $openHomeDetail;


                    }
                }

                //Always check for emptiness
                if(empty($openHomeTimes))
                {
                    $message = 'No Open Homes';

                    $openHomeTimes = $message;
                }
于 2012-12-27T23:27:20.630 回答