警告:非法偏移类型PHP错误

时间:2018-01-14 02:34:24

标签: php

我在第9行和第19行),);上收到了偏移错误,但我不确定原因。我不认为我在我的阵列上调用一个对象,因为$ i和$ j都是数字。这都是为了在btw表格中显示图像。提前谢谢!

代码:

            $hero = Array (
            [0] => Array 
                (
                    array("Superman", "<img src='https://upload.wikimedia.org/wikipedia/en/e/eb/SupermanRoss.png' alt='Superman' title='Superman' style='height:150px;'>", 1),
                    array("Batman", "<img src='https://img00.deviantart.net/d9fa/i/2017/079/7/d/batman___transparent_by_asthonx1-db2yliv.png' alt='Batman' title='Batman' style='height:150px;'>", 1),
                    array("Flash", "<img src='http://www.pngmart.com/files/2/Flash-Transparent-PNG.png' alt='Flash' title='Flash' style='height:150px;'>", 1),
                    array("Aquaman", "<img src='https://vignette.wikia.nocookie.net/deathbattle/images/8/8f/Aquaman_transparent_by_asthonx1-dakip9a.png/revision/latest?cb=20170702181517' alt='Aquaman' title='Aquaman' style='height:150px;'>", 1),
                    array("Green Lantern", "<img src='https://orig00.deviantart.net/d594/f/2017/197/4/d/green_lantern_request___transparent_background_by_camo_flauge-dbgi25l.png' alt='Green Lantern' title='Green Lantern' style='height:150px;'>", 1)
                ), //error here
            [1] => Array 
                (
                    array("Wolverine", "<img src='https://orig00.deviantart.net/4580/f/2016/274/1/f/wolverine___transparent_by_asthonx1-dajhanh.png' alt='Wolverine' title='Wolverine' style='height:150px;'>", 2),
                    array("Ironman", "<img src='http://www.pngmart.com/files/3/Iron-Man-PNG-File.png' alt='Ironman' title='Ironman' style='height:150px;'>", 2),
                    array("Ant Man", "<img src='https://orig00.deviantart.net/7ec8/f/2016/092/d/f/ant_man_by_cptcommunist-d9xiez4.png' alt='Ant man' title='Ant man' style='height:150px;'>", 2),
                    array("Thor", "<img src='http://www.freepngimg.com/download/thor/3-2-thor-transparent.png' alt='Thor' title='Thor' style='height:150px;'>", 2),
                    array("Hulk", "<img src='https://img00.deviantart.net/f71e/i/2016/274/7/8/hulk___transparent_by_asthonx1-dajha0a.png' alt='Hulk' title='Hulk' style='height:150px;'>", 2)
                )

            ); //error here

        if ($_POST['battle']){
            $n1 = rand(0,5);
            $n2 = rand(6,9);
            echo $n1 , $n2;
            for($i=0;$i<count($hero);$i++) {
                echo('<tr>');
                for($j=0;$j<count($hero[$i]);$j++) {
                    echo('<td>' . $hero[$i][$j][1] . '</td>');
                } 
                echo('</tr>');
            }
        }               

2 个答案:

答案 0 :(得分:0)

使用[0] =&gt;数组(...)正在尝试将数组定义为键。键可以是字母数字,如关联数组或数字索引数组。

因此,您可以让数组决定数字索引,因为您从0开始。

$hero = array(
    array
    (
        array("Superman", "<img src='https://upload.wikimedia.org/wikipedia/en/e/eb/SupermanRoss.png' alt='Superman' title='Superman' style='height:150px;'>", 1),
        array("Batman", "<img src='https://img00.deviantart.net/d9fa/i/2017/079/7/d/batman___transparent_by_asthonx1-db2yliv.png' alt='Batman' title='Batman' style='height:150px;'>", 1),
        array("Flash", "<img src='http://www.pngmart.com/files/2/Flash-Transparent-PNG.png' alt='Flash' title='Flash' style='height:150px;'>", 1),
        array("Aquaman", "<img src='https://vignette.wikia.nocookie.net/deathbattle/images/8/8f/Aquaman_transparent_by_asthonx1-dakip9a.png/revision/latest?cb=20170702181517' alt='Aquaman' title='Aquaman' style='height:150px;'>", 1),
        array("Green Lantern", "<img src='https://orig00.deviantart.net/d594/f/2017/197/4/d/green_lantern_request___transparent_background_by_camo_flauge-dbgi25l.png' alt='Green Lantern' title='Green Lantern' style='height:150px;'>", 1)
    ),
    array
    (
        array("Wolverine", "<img src='https://orig00.deviantart.net/4580/f/2016/274/1/f/wolverine___transparent_by_asthonx1-dajhanh.png' alt='Wolverine' title='Wolverine' style='height:150px;'>", 2),
        array("Ironman", "<img src='http://www.pngmart.com/files/3/Iron-Man-PNG-File.png' alt='Ironman' title='Ironman' style='height:150px;'>", 2),
        array("Ant Man", "<img src='https://orig00.deviantart.net/7ec8/f/2016/092/d/f/ant_man_by_cptcommunist-d9xiez4.png' alt='Ant man' title='Ant man' style='height:150px;'>", 2),
        array("Thor", "<img src='http://www.freepngimg.com/download/thor/3-2-thor-transparent.png' alt='Thor' title='Thor' style='height:150px;'>", 2),
        array("Hulk", "<img src='https://img00.deviantart.net/f71e/i/2016/274/7/8/hulk___transparent_by_asthonx1-dajha0a.png' alt='Hulk' title='Hulk' style='height:150px;'>", 2)
    ),
);

或者您可以更明确地定义初始外部数组索引(如关联数组)并使用...

$hero = array(
    0 => array
    (
        array("Superman", "<img src='https://upload.wikimedia.org/wikipedia/en/e/eb/SupermanRoss.png' alt='Superman' title='Superman' style='height:150px;'>", 1),
        array("Batman", "<img src='https://img00.deviantart.net/d9fa/i/2017/079/7/d/batman___transparent_by_asthonx1-db2yliv.png' alt='Batman' title='Batman' style='height:150px;'>", 1),
        array("Flash", "<img src='http://www.pngmart.com/files/2/Flash-Transparent-PNG.png' alt='Flash' title='Flash' style='height:150px;'>", 1),
        array("Aquaman", "<img src='https://vignette.wikia.nocookie.net/deathbattle/images/8/8f/Aquaman_transparent_by_asthonx1-dakip9a.png/revision/latest?cb=20170702181517' alt='Aquaman' title='Aquaman' style='height:150px;'>", 1),
        array("Green Lantern", "<img src='https://orig00.deviantart.net/d594/f/2017/197/4/d/green_lantern_request___transparent_background_by_camo_flauge-dbgi25l.png' alt='Green Lantern' title='Green Lantern' style='height:150px;'>", 1)
    ),
    1 => array
    (
        array("Wolverine", "<img src='https://orig00.deviantart.net/4580/f/2016/274/1/f/wolverine___transparent_by_asthonx1-dajhanh.png' alt='Wolverine' title='Wolverine' style='height:150px;'>", 2),
        array("Ironman", "<img src='http://www.pngmart.com/files/3/Iron-Man-PNG-File.png' alt='Ironman' title='Ironman' style='height:150px;'>", 2),
        array("Ant Man", "<img src='https://orig00.deviantart.net/7ec8/f/2016/092/d/f/ant_man_by_cptcommunist-d9xiez4.png' alt='Ant man' title='Ant man' style='height:150px;'>", 2),
        array("Thor", "<img src='http://www.freepngimg.com/download/thor/3-2-thor-transparent.png' alt='Thor' title='Thor' style='height:150px;'>", 2),
        array("Hulk", "<img src='https://img00.deviantart.net/f71e/i/2016/274/7/8/hulk___transparent_by_asthonx1-dajha0a.png' alt='Hulk' title='Hulk' style='height:150px;'>", 2)
    )

);

答案 1 :(得分:-1)

这可能是由于众多事情造成的。由于您没有提供任何错误,我只能分析您所提供的内容。

1,
你的$ hero是一个包含2个数组的数组,它们保存数组。确保您正确访问这些内容。

2,
确保$ _POST [&#39;战斗&#39;]被设置。你不能依赖它被设定甚至是真或假。始终检查外部(和内部)输入。

3,
这条线: var_dump($array); 是错的。你有英雄数组,因为$ i和英雄数组的部分为$ j。但是,这不会访问任何超级英雄,因为您没有引用任何超级英雄。该错误可能来自于尝试定期打印阵列。如果您要打印包含<pre><code>标签的数组{{1}},建议您使用。