PHP警告:偏移量类型非法

时间:2018-10-27 00:22:53

标签: php multidimensional-array

我正在尝试启动此数组,并且一直保持该非法偏移量类型。老实说,我什至不确定我是否正确设置了阵列,任何帮助都将是一件好事!

$device_changes = array
(
    [0] => array
        (
            ['device'] => "Connect",
            ['added'] => 0,
            ['removed'] => 0,
            ['net_change'] => 0,
            ['percent_added'] => 0,
            ['percent_removed'] => 0,
        ),
    [1] => array
        (
            ['device'] => "Camera",
            ['added'] => 0,
            ['removed'] => 0,
            ['net_change'] => 0,
            ['percent_added'] => 0,
            ['percent_removed'] => 0,
        ),
    [2] => array
        (
            ['device'] => "BATlte",
            ['added'] => 0,
            ['removed'] => 0,
            ['net_change'] => 0,
            ['percent_added'] => 0,
            ['percent_removed'] => 0,
        ),
    [3] => array
        (
            ['device'] => "BATwifi";
            ['added'] => 0,
            ['removed'] => 0,
            ['net_change'] => 0,
            ['percent_added'] => 0,
            ['percent_removed'] => 0,
        ),
);

3 个答案:

答案 0 :(得分:3)

按键不需要在方括号内

所以您应该拥有:

$device_changes = array
    (
        0 => array
            (
                'device' => "Connect",
                'added' => 0,
                'removed' => 0,
                'net_change' => 0,
                'percent_added' => 0,
                'percent_removed' => 0,
            ),
        1 => array
            (
                'device' => "Camera",
                'added' => 0,
                'removed' => 0,
                'net_change' => 0,
                'percent_added' => 0,
                'percent_removed' => 0,
            ),
        2 => array
            (
                'device' => "BATlte",
                'added' => 0,
                'removed' => 0,
                'net_change' => 0,
                'percent_added' => 0,
                'percent_removed' => 0,
            ),
        3 => array
            (
                'device' => "BATwifi",
                'added' => 0,
                'removed' => 0,
                'net_change' => 0,
                'percent_added' => 0,
                'percent_removed' => 0,
            ),
    );

请注意,BATwifi后应在行尾,而不是;也是:)

编辑:

您可能对方括号感到困惑,因为可以使用array()[]来设置数组。如:

$device_changes = [
    0 => [
        'device' => 'Connect',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    1 => [
        'device' => 'Camera',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    2 => [
        'device' => 'BATlte',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    3 => [
        'device' => 'BATwifi',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
];

您甚至可以摆脱数字键,因为默认情况下,它们将设置为您所使用的数字,从0开始并以1递增:

$device_changes = [
    [
        'device' => 'Connect',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    [
        'device' => 'Camera',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    [
        'device' => 'BATlte',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    [
        'device' => 'BATwifi',
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
];

答案 1 :(得分:0)

您将printf的输出与数组的定义混合在一起。如果要定义数组,则不能使用[0]['device']作为数组键,因为这是一个具有单个元素的数组。相反,在上面的示例中,您应该使用0'device'

此外,如果它只是一个自动递增编号,则可以完全跳过该键。

$device_changes = [
    [
        'device' => "Connect",
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    [
        'device' => "Camera",
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    [
        'device' => "BATlte",
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
    [
        'device' => "BATwifi";
        'added' => 0,
        'removed' => 0,
        'net_change' => 0,
        'percent_added' => 0,
        'percent_removed' => 0,
    ],
];

答案 2 :(得分:0)

您可以使用我刚刚写的这个转换器“ thingy”

https://github.com/ArtisticPhoenix/MISC/tree/master/Lexers/OutputConverter

但是,如果使用print_r,则必须“撤消”所做的任何操作。即

  • 将单词*aa = 20; 大写(否则它不会检测到它的print_r)
  • 删除动词Array'",

所以看起来像这样:

;

然后它将为您提供:

Array
(
    [0] => Array
        (
            [device] => Connect
            [added] => 0
            [removed] => 0
            [net_change] => 0
            [percent_added] => 0
            [percent_removed] => 0
        )
    [1] => Array
        (
            [device] => Camera
            [added] => 0
            [removed] => 0
            [net_change] => 0
            [percent_added] => 0
            [percent_removed] => 0
        )
    [2] => Array
        (
            [device] => BATlte
            [added] => 0
            [removed] => 0
            [net_change] => 0
            [percent_added] => 0
            [percent_removed] => 0
        )
    [3] => Array
        (
            [device] => BATwifi
            [added] => 0
            [removed] => 0
            [net_change] => 0
            [percent_added] => 0
            [percent_removed] => 0
        )
)

它也可以在array ( 0 => array ( 'device' => 'Connect', 'added' => 0, 'removed' => 0, 'net_change' => 0, 'percent_added' => 0, 'percent_removed' => 0 ), 1 => array ( 'device' => 'Camera', 'added' => 0, 'removed' => 0, 'net_change' => 0, 'percent_added' => 0, 'percent_removed' => 0 ), 2 => array ( 'device' => 'BATlte', 'added' => 0, 'removed' => 0, 'net_change' => 0, 'percent_added' => 0, 'percent_removed' => 0 ), 3 => array ( 'device' => 'BATwifi', 'added' => 0, 'removed' => 0, 'net_change' => 0, 'percent_added' => 0, 'percent_removed' => 0 ) ) 上运行,并且可以检测var_dumpvar_exportvar_dump之间的差异。它可以处理对象,嵌套等。甚至可以像var_export ... :-D

一样缩进

我花了几天时间,因为我厌倦了不得不弄乱人们喜欢在这里发布的数组(不在var_export中)。

使用它非常容易,它具有一个带有一个按钮的Web UI和一个文本区域,可以粘贴和复制,甚至简单。

对不起,源代码太长,无法包含在帖子中,所以我喜欢它有点凌乱,但是我一直在尝试完成我的网站(在上面设置了SCSS解析器后稍作休息)。太糟糕了。

迟早我会把它放在那里,但这是一个wordpress驱动的站点,所以我可能不得不为它和所有爵士乐做一个简短的代码。

享受