简体   繁体   中英

PHP Warning: Illegal Offset type

I'm trying get this array started and I keep get that illegal offset type. I'm honestly not even sure if I'm setting up my array correctly any help would be nice!

$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,
        ),
);

The keys do not need square brackets around them.

So you should have:

$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,
            ),
    );

Note after BATwifi it should be , on the end of the line and not a ; too :)

Edit:

You may have got confused with the square brackets as arrays can be set using array() or [] . Such as:

$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,
    ],
];

You could even get rid of the numeric keys as by default they will set to what you have used, start at 0 and increment by one:

$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,
    ],
];

You mixed the output of printf with the definition of an array. If you want to define an array, you can't use [0] or ['device'] as an array key, as this is an array with a single element. Instead, you should use 0 or 'device' in the above example.

In addition, you could skip the key completely, if it is just a auto increment number.

$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,
    ],
];

You can use this converter "thingy" I just wrote

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

But you will have to "undo" whatever you did, if you made it with print_r. Namely

  • capitalize the word Array (or it will not detect it's print_r)
  • remove puctuations ' , " , , and ;

So it looks like this:

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
        )
)

Then it will give you this:

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
    )
)

It also works on var_dump and can detect the difference between, var_export , var_dump and print_r . It can handle objects, nesting etc. It even indents exactly like var_export ... :-D

I been working on it a few days, because i get sick of having to mess with arrays done (not in var_export) that people like to post on here.

It's pretty easy to use it has a web UI with one button and a textarea to paste and copy from, simple even.

Sorry the source code is too long to include in the post, and it's a bit messier then I like, but I been trying to get my website done (taking a break after setting up a SCSS parser on it) so ... too bad.

Sooner or later I will put it on there, but it's a wordpress driven site so I will probably have to make a short code for it and all that jazz.

Enjoy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM