• dan@upvote.au
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      The best programming languages let you pick if arrays are base 0 or 1. Shout out to Option Base 1.

        • dan@upvote.au
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          PHP causes so many issues because of the fact that it uses the same syntax for both arrays and dictionaries/maps. [] (or array() in older PHP versions) always JSON encodes to [] because there’s no way to tell if it’s supposed to represent an empty array or an empty dictionary.

          • xmunk@sh.itjust.works
            link
            fedilink
            arrow-up
            2
            arrow-down
            1
            ·
            1 year ago

            Eh. It works just fine. I agree JSON encoding is weird but if you use it the recommended way (just use a StdClass object and throw on the fields you need) it works fine. Someone added half assed support for weird PHP arrays that usually works and is generally good enough.

            • dan@upvote.au
              link
              fedilink
              arrow-up
              2
              ·
              1 year ago

              stdClass is for objects. I’m talking about maps. I guess you could use stdClass for maps too, but that feels weird.

              • xmunk@sh.itjust.works
                link
                fedilink
                arrow-up
                1
                ·
                1 year ago

                If you read the docs for json_decode, the default behavior is to operate with stdClass - I too prefer working with associative arrays because of their power but… that kind of just reinforces how useful they are. PHP can give you the full Java class system if you want… it’s just a pain and useless in most circumstances so we all just toss around associative arrays.

      • palordrolap@kbin.social
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

        Perl had $[ (yes, just those two characters) to allow setting of the first array index, but it’s a fatal error to put anything but 0 into it now.

        Of course, since Perl arrays can be negative subscripted from the last element, you can stand on your head and index the array negatively from the wrong end. -1, -2, -3 etc. Use unshift to put things on the beginning (logical “end”) of what you’re doing rather than the usual push. Presto, a 1-based array (up to sign, anyway).

        You may wish to file this under “stupid, but it works”.

        People with more time / more need for something that looks professional, would probably be better off writing something that uses tie instead. (Or, yes, yes, a different language entirely, hush now.)