douzhaolu4780 2018-01-23 11:15
浏览 168

访问数组值时非法字符串偏移量

I have the following array called $shops:

Array
(
    [0] => Array
        (
            [id] => 10
            [open] => Array
                (
                    [0] => Array
                        (
                            [day] => Monday
                            [start] => 09:00:00
                            [end] => 17:00:00
                        )

                    [1] => Array
                        (
                            [day] => Tuesday
                            [start] => 09:00:00
                            [end] => 17:00:00
                        )

                    [2] => Array
                        (
                            [day] => Thursday
                            [start] => 09:00:00
                            [end] => 17:00:00
                        )
                )
        )
)

When I loop through it similar to the following I get Illegal string offset 'day'

foreach($shops as $shop)
{
    foreach($shop['open'] as $open)
    {
        print_r($open);
        echo $open['day'];
    }
}

The print_r($open) appears correct:

Array
(
    [day] => Monday
    [start] => 09:00:00
    [end] => 17:00:00
)

I must be doing something stupid. What's the problem?

  • 写回答

2条回答 默认 最新

  • duan2428 2018-01-23 11:35
    关注

    I have write these code in details with check all iteration. In second loop first iteration has no day index and it's value is 10. you can check output

    $count=0;
    echo "<pre>";
    foreach ($shops as $shop) {
        print_r($shop);
        echo "first
    ";
        foreach($shop as $open)
        {
            echo "second
    ";
            print_r($open);
            if(isset($open[0]))
            {
                foreach ($open as  $value) {
                    print_r($value['day']);
                }
            }
    
    
        }
    
    }
    

    Also you can Check this code. and output of this code is Here . further you can split this in nested foreach loop

    <?php
    
    $shops = Array
    (
        0 => Array
            (
                'id' => 10,
                'open' => Array
                    (
                        0 => Array
                            (
                                'day' => 'Monday',
                                'start' => '09:00:00',
                                'end' => '17:00:00',
                            ),
    
                        1 => Array
                            (
                                'day' => 'Tuesday',
                                'start' => '09:00:00',
                                'end' => '17:00:00',
                            ),
    
                        2 => Array
                            (
                                'day' => 'Thursday',
                                'start' => '09:00:00',
                                'end' => '17:00:00',
                            ),
                    )
            )
    );
    
    
    foreach ($shops[0]['open'] as $open) {
        print_r($open['day']);
        echo "<br>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿