Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Cannot use string offset as an array

    Scheduled Pinned Locked Moved Development
    13 Posts 4 Posters 2.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      coreybrett
      last edited by

      The following line...

      $config['installedpackages']['zerotier']['config']['enabled'] = true;
      

      produces the following error when used in a packages GUI code...

      Fatal error: Uncaught Error: Cannot use string offset as an array
      

      However, I can exec that exact line using the pfSense php shell without an issue.

      What string offset is it talking about?

      1 Reply Last reply Reply Quote 0
      • jimpJ
        jimp Rebel Alliance Developer Netgate
        last edited by

        It's a bug in the package that needs fixed for PHP 7.2.

        Contact the author of that package to have them fix it. That is not a package that is present in the pfSense repository, or one developed/maintained by Netgate.

        Remember: Upvote with the ๐Ÿ‘ button for any user/post you find to be helpful, informative, or deserving of recognition!

        Need help fast? Netgate Global Support!

        Do not Chat/PM for help!

        1 Reply Last reply Reply Quote 0
        • C
          coreybrett
          last edited by

          I am actually trying to work on the package myself. Is there any documentation on the config system and how to interact with it? The functions to use and such.

          1 Reply Last reply Reply Quote 0
          • jimpJ
            jimp Rebel Alliance Developer Netgate
            last edited by

            No, but that's not a config or pfSense issue -- that's a general issue with PHP 7.x and multi-level hashed arrays like that. You have to initialize each layer, you can't assume any part of it is an array, or you get errors when trying to perform some operations.

            Remember: Upvote with the ๐Ÿ‘ button for any user/post you find to be helpful, informative, or deserving of recognition!

            Need help fast? Netgate Global Support!

            Do not Chat/PM for help!

            1 Reply Last reply Reply Quote 0
            • C
              coreybrett
              last edited by

              I see the following line in another package ...

              $a_mailers = getarraybyref($config, 'installedpackages', 'haproxy', 'email_mailers', 'item');
              

              would I need to do something like that ?

              P 1 Reply Last reply Reply Quote 0
              • P
                PiBa @coreybrett
                last edited by

                @coreybrett
                Yes that would solve the issue :)

                1 Reply Last reply Reply Quote 0
                • C
                  coreybrett
                  last edited by

                  Is ['config'][0]['enable'] a requirement for storing the state of a package?

                  P 1 Reply Last reply Reply Quote 0
                  • P
                    PiBa @coreybrett
                    last edited by

                    @coreybrett
                    I think not. Afaik the ['config'][0] is only for trying to support a remnant of a old config format iirc.

                    1 Reply Last reply Reply Quote 0
                    • jimpJ
                      jimp Rebel Alliance Developer Netgate
                      last edited by

                      If a package uses XML forms to store data they are automatically put in that style of array. For example, if you use pkg_edit.php?xml=foo.xml for settings with multiple values (lists of things, for example) then it would be stored with that last [0] as the item id, so you'd have 0, 1, 2 and so on for each separate entry. It's just easier to use the same format for the other style as well for consistency, even if it's always 0.

                      Remember: Upvote with the ๐Ÿ‘ button for any user/post you find to be helpful, informative, or deserving of recognition!

                      Need help fast? Netgate Global Support!

                      Do not Chat/PM for help!

                      1 Reply Last reply Reply Quote 0
                      • J
                        JustLeo
                        last edited by

                        Hello, I'm trying to do something similar on my side but from the comments i was not able to understand what's wrong in my code and how to make it work. So here is one of the lines I'm trying to execute:

                        $config['aliases']['alias'][1]['name'] = 'LANNetworks';
                        

                        And this is producing the following error:

                        Fatal error: Uncaught Error: Cannot use string offset as an array in /usr/local/sbin/pfSsh.php(371) : eval()'d code:4
                        

                        This peace of code works fine on previous version of pfsense 2.4.3 but in 2.4.4 it's giving me this problem. Would somebody be able to help me? Thanks in advance.

                        P 1 Reply Last reply Reply Quote 0
                        • P
                          PiBa @JustLeo
                          last edited by PiBa

                          @justleo
                          You would need to write something like this:

                          if (!is_array($config['aliases'])) $config['aliases'] = array();
                          if (!is_array($config['aliases']['alias'])) $config['aliases']['alias'] = array();
                          if (!is_array($config['aliases']['alias'][1])) $config['aliases']['alias'][1] = array();
                          $config['aliases']['alias'][1]['name'] = 'LANNetworks';
                          

                          Or imho easier:

                          $myalias = &getarraybyref($config, 'aliases', 'alias', 1);
                          $myalias['name'] = 'LANNetworks';
                          
                          1 Reply Last reply Reply Quote 0
                          • jimpJ
                            jimp Rebel Alliance Developer Netgate
                            last edited by

                            Or:

                            init_config_arr(array('aliases', 'alias', 1));
                            $config['aliases']['alias'][1]['name'] = 'LANNetworks';
                            

                            Remember: Upvote with the ๐Ÿ‘ button for any user/post you find to be helpful, informative, or deserving of recognition!

                            Need help fast? Netgate Global Support!

                            Do not Chat/PM for help!

                            1 Reply Last reply Reply Quote 1
                            • J
                              JustLeo
                              last edited by

                              @jimp Thanks for your quick help, I was able to make it work with your second suggention:

                              init_config_arr(array('aliases', 'alias', 0));
                              $config['aliases']['alias'][0]['name'] = 'LANNetworks';
                              $config['aliases']['alias'][0]['address'] = 'CIDR';
                              $config['aliases']['alias'][0]['descr'] = 'SomeDescription';
                              $config['aliases']['alias'][0]['type'] = 'network';
                              
                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.