Php 无法将标量值用作数组(实际上我正在尝试访问不存在的数组键)

Php 无法将标量值用作数组(实际上我正在尝试访问不存在的数组键),php,arrays,Php,Arrays,我更新了问题标题,因为我希望得到的错误是“您试图访问一个不存在的密钥”,在eof上,您的评论者让我看到了这一点。无论如何,这是我最初的问题: 我一直在犯错误 警告:无法将标量值用作 从下面第二行开始getContainerSleepSettings()返回一个数组,但这应该不是问题,对吗 $helper = $this->get('biztv.helper.globalHelper'); $containers = $helper->getContainers();

我更新了问题标题,因为我希望得到的错误是“您试图访问一个不存在的密钥”,在eof上,您的评论者让我看到了这一点。无论如何,这是我最初的问题:

我一直在犯错误

警告:无法将标量值用作

从下面第二行开始
getContainerSleepSettings()
返回一个数组,但这应该不是问题,对吗

   $helper = $this->get('biztv.helper.globalHelper');
    $containers = $helper->getContainers();

    //setup helper array with id's corresponding to the $containers (array of container entities)
    $containerHelper = array();
    foreach ($containers as $c) {
        $containerHelper[] = $c->getId();

        //Find out if sleeping
        $sleepSettings = $this->getContainerSleepSettings($c);
        var_dump( $containerHelper[$c->getId()]['wakeup'] = $sleepSettings['wakeup_s'] );
        var_dump( $containerHelper[$c->getId()]['sleep_s'] = $sleepSettings['sleep_s'] );

        if ( $sleepSettings['wakeup'] < date('U') && date('U') < $sleepSettings['sleep'] ) {
            $containerHelper[$c->getId()]['asleep'] = true; 
        }

        //Find out if online
        //$containerHelper[$c->getId()]['online'] = '$this->getOnlineAction( $c->getId() )';


        //find out if inherit layout/content



        //Find out if container receives mediasync



    }

    print_r($containerHelper);
    die;


private function getContainerSleepSettings($container) {

    //First find a container with sleepSettings
    while(!$container->getHourEnd() || !$container->getHourStart() ) {

        if ( $container->getParent() ) {
            $container = $container->getParent();
        }
        else {
            return null; //Infinity limited by level of parents - once no more parents we return null.
        }
    }

    //If we did find a parent with sleepSettings on it:


    $sleep['wakeup'] = $container->getHourEnd();
    $sleep['sleep'] = $container->getHourStart();

    if ($sleep['wakeup']) {
        $sleep['wakeup_s'] = $sleep['wakeup']->format('H:i');
    }
    else {
        $sleep['wakeup_s'] = '-';
    }


    if ($sleep['sleep']) {
        $sleep['sleep_s'] = $sleep['sleep']->format('H:i');
    }
    else {
        $sleep['sleep_s'] = '-';
    }

    return $sleep;          

}
$helper=$this->get('biztv.helper.globalHelper');
$containers=$helper->getContainers();
//设置id与$containers对应的助手数组(容器实体数组)
$containerHelper=array();
foreach(集装箱为$c){
$containerHelper[]=$c->getId();
//看看你是否在睡觉
$sleepSettings=$this->getContainerSleepSettings($c);
var_dump($containerHelper[$c->getId()['wakeup']=$sleepSettings['wakeup']);
var_dump($containerHelper[$c->getId()]['sleep_s']=$sleepSettings['sleep_s']);
if($sleepSettings['wakeup']getId()]['sleep']=true;
}
//查看是否在线
//$containerHelper[$c->getId()]['online']='$this->getOnlineAction($c->getId())';
//了解是否继承布局/内容
//了解容器是否接收mediasync
}
印刷费($containerHelper);
死亡
私有函数getContainerSleepSettings($container){
//首先找到一个具有睡眠设置的容器
而(!$container->gethourrend()|;!$container->getHourStart()){
如果($container->getParent()){
$container=$container->getParent();
}
否则{
return null;//无限受限于父级-一旦不再有父级,我们将返回null。
}
}
//如果我们确实找到了带有睡眠设置的家长:
$sleep['wakeup']=$container->getHourEnd();
$sleep['sleep']=$container->getHourStart();
如果($sleep['wakeup'])){
$sleep['wakeup_'s]=$sleep['wakeup']->格式('H:i');
}
否则{
$sleep['wakeup_s']='-';
}
如果($sleep['sleep'])){
$sleep['sleep_'s]=$sleep['sleep']->格式('H:i');
}
否则{
$sleep['sleep_s']='-';
}
返回$sleep;
}

在访问返回值之外的值之前,您需要检查它是否为数组

$sleepSettings = $this->getContainerSleepSettings($c);
if ( is_array($sleepSettings) ) {
    var_dump( $containerHelper[$c->getId()]['wakeup'] = $sleepSettings['wakeup_s'] );
    var_dump( $containerHelper[$c->getId()]['sleep_s'] = $sleepSettings['sleep_s'] );

    if ( $sleepSettings['wakeup'] < date('U') && date('U') < $sleepSettings['sleep'] ) {
        $containerHelper[$c->getId()]['asleep'] = true; 
    }
} else {
   // $sleepSettings  is null
   // do something else..
}
$sleepSettings=$this->getContainerSleepSettings($c);
if(is_数组($sleepSettings)){
var_dump($containerHelper[$c->getId()['wakeup']=$sleepSettings['wakeup']);
var_dump($containerHelper[$c->getId()]['sleep_s']=$sleepSettings['sleep_s']);
if($sleepSettings['wakeup']getId()]['sleep']=true;
}
}否则{
//$sleepSettings为空
//做点别的。。
}

我的数组中不能有null吗?可以,但是当它返回null并且您试图访问任何密钥时,您将无法获得该密钥,并且它将触发消息
警告:不能在
中使用标量值作为数组。实际上,您需要指出在哪一行获得此错误?谢谢。添加了一个条件:if($sleepSettings null){$containerHelper[$c->getId()]['wakeup']=$sleepSettings['wakeup_s'];$containerHelper[$c->getId()]['sleep_s']=$sleepSettings['sleep_s'];}
$sleep = array();
$sleep['wakeup'] = $container->getHourEnd();