: Multidimensional Arrays the easy way  ( 433 )

markem

  • Administrator
  • Hero Member
  • *****
  • : 2,128
Multidimensional Arrays the easy way
« : February 24, 2017, 11:52:20 AM »
I have been playing around with trying to set up a logical, easy to use, method of handling multidimensional arrays without having to do all of the...

$a->Array['this']['is']['a']['test'];

kind of things. I wanted a way to reference them easily. I think I have it. Instead of having to do the above, all you do is this:

$a->this_is_a_test = 5;

OR - you do this:

$a->set("this.is.a.test", 5);

The "set" function then takes the string and breaks it up on non-word characters. (So in this case - the period.) What you wind up with is something like this:

:
Array
(
    [cleric] => Array
        (
            [ability] => cleric
            [level] => 1
            [mana] => 1
            [index] => Primary
            [religion] => The One True God
            [spell] => Array
                (
                    [1] => Array
                        (
                            [title] => Heal
                            [level] => 1
                            [desc] => Does 1d8 of healing per level
                            [dice] => 1d8
                        )

                )

        )

)

(Of course there are several statements like $a->prefix("cleric"); and $a->ability="cleric"; statements that set all of that. The main thing is - this makes it really easy to add, delete, modify, and get the array information without having to do the really long "['this']['is']['a']['test']" which can lead to all sorts of errors.

markem

  • Administrator
  • Hero Member
  • *****
  • : 2,128
Re: Multidimensional Arrays the easy way
« #1 : February 24, 2017, 12:00:02 PM »
ARGH! It got me twice! Ugh. :-(

Ok, here is another output on my working on this stuff. The nice thing is - it is easy to store/get the information. You just use JSON_ENCODE() on the array to create one giant string and then FILE_PUT_CONTENTS(<FILE>,<DATA>) to save it. To get it back you just do FILE_GET_CONTENTS(<FILE>) and JSON_DECODE().  Here is another output:

:
Array
(
    [cleric] => Array
        (
            [ability] => cleric
            [level] => 1
            [mana] => 1
            [skill] => Primary
            [notes] => Primary skills operate at 1:1.
            [religion] => The One True God
            [spells] => Array
                (
                    [Heal] => Array
                        (
                            [level] => 1
                            [desc] => Does 1d8 of healing per level
                            [dice] => 1d8
                        )

                )

        )

)
Array
(
    [cleric] => Array
        (
            [ability] => cleric
            [level] => 1
            [mana] => 1
            [skill] => Primary
            [notes] => Primary skills operate at 1:1.
            [religion] => The One True God
            [spells] => Array
                (
                    [Heal] => Array
                        (
                            [level] => 1
                            [desc] => Does 1d8 of healing per level
                            [dice] => 1d8
                        )

                )

        )

    [fighter] => Array
        (
            [level] => 1
            [type] => generic
            [skill] => Secondary skills operate at 1:2
            [weapons] => Array
                (
                    [sword] => Array
                        (
                            [damage] => 1d8
                        )

                )

        )

    [doctor] => Array
        (
            [skill] => Tertiary
            [notes] => Tertiary skills operate at 1:3
            [level] => 1
            [desc] => The Doctor skill allows you to do non-magical healing at 1d8. This means disfigurement happens.
            [dice] => 1d8
        )

)
isset: TRUE
unset: TRUE

markem

  • Administrator
  • Hero Member
  • *****
  • : 2,128
Re: Multidimensional Arrays the easy way
« #2 : February 28, 2017, 02:21:14 PM »
Well, I have progressed quite quickly on this multidimensional array stuff.  I revamped it so there is just one routine that everything goes through to get, set, delete, whatever. So now I'm trying to use it to edit the abilities. I'm also using JSON_ENCODE and JSON_DECODE. In this way I can load all of the abilities up and split them back up into the array I had them in just by doing the one call to JSON_DECODE.  There are a lot of abilities and I am having to sort them in to their proper location. I don't know which is worse - putting them in one by one or putting all of them in from a list and then organizing them. :-/