0

I stumbled upon one problem regarding too complicated(at least in my point of view) data structures stored in option field.

Right now I have an array of collections and within each other there is another sub-collection (as depicted below). In my opinion it looks too complicated, especially when one wants to store some value inside. More or less it would look like:

$collection = get_option('collection_setting');
$collection[1][elements][0][name] = "ChangedExample";

And this can get much worse when we would like to introduce another array inside of collection. Plus I think it can get quite sloppy if one uses like 100 entries per array.

My question is how could this be improved? Storing it into two different option fields - one for collection, second for sub-collections and then somehow trying to connect this within code? Or maybe this is perfectly OK?

Of course I assume that creating another table in DB is not the most elegant way.

Array
(
  [0] => Array
      (
        [name] => collection_name
        [speed] => 200
        [id] => collection_name_1
        [max_elements_per_page] => 13
        [elements] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [url] => http://www.example1.org
                        [name] => Example1
                    )

                [1] => Array
                    (
                        [id] => 2
                        [url] => http://www.example2.org
                        [name] => Example2
                    )

                [2] => Array
                    (
                        [id] => 3
                        [url] => http://www.example3.org
                        [name] => Example4
                    )

            )

    )

[1] => Array
    (
        [name] => collection_name_2
        [speed] => 200
        [id] => collection_name_2
        [max_elements_per_page] => 13
        [elements] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [url] => http://www.example1.org
                        [name] => Example1
                    )

                [1] => Array
                    (
                        [id] => 2
                        [url] => http://www.example2.org
                        [name] => Example2
                    )

                [2] => Array
                    (
                        [id] => 3
                        [url] => http://www.example3.org
                        [name] => Example4
                    )

            )

    )

)

For example when I have setting page on which I want to edit only the first element of collection then with the moment I'm saving it, Wordpress overwrites whole option with only values submitted with form, erasing second element from collection (here $collection[1] is erased).

What is the way out? I'm thinking of using validate_options function( which is receiving data from setting page). I could retrieve collection_setting, modify it accordingly and returning modified collection_setting.

register_setting('collection-settings-group', 'collection_setting', 'validate_options');

But I think it's too much work and I'm sure there is some better solution for this.

5
  • 3
    There is nothing wrong with multidimensional arrays, if you want something more elegant you would have to use a native PHP array function (maybe a class to wrap around it) or have a look at wp_list_pluck codex.wordpress.org/Function_Reference/wp_list_pluck
    – Wyck
    Dec 26, 2012 at 3:09
  • 1
    +1 with @Wyck. It's always good to store a single option as a single key and a single value, no matter how deep that array is. Infact many plugins do it this way. One thing you can do is, when a value in that option array is no more needed, remove it and update the option. Dec 26, 2012 at 4:56
  • 1
    I'd have to know how the array is used. Try to forget about how complicated it looks and try to figure out if there is something about it that is dis-functional? If you can't think of anything, leave it alone. And what does "sloppy" mean? Why is an array with 100 elements sloppy?
    – s_ha_dum
    Dec 26, 2012 at 5:35
  • Im generally using it to store series of pictures which are categorized into groups. Each group has it's own page on admin page(I distinguish them by group_id parameter which is provided in URL). I want to have ability to modify each one, but till now it's quite hard since every submit overwrites whole setting with collection element I just edited.
    – dfens
    Dec 28, 2012 at 0:01
  • From your example it looks more like content than options and may be better suited as a custom post type / taxonomy / meta data
    – Milo
    Dec 28, 2012 at 0:25

1 Answer 1

1

This is a typical situation for using a class to manage a data structure. You got a complex data structure and you need some functions to retrieve and modify the data structure. This is easy made with a class.

The class get the data structure from the database and have some methods to get parts of the data structure or modify them.

I wrote a sample class, you can extend or modify it with your own methods. Using the class is very simple:

  <?php
  // creating an instance of the class and retrieve the data from db
  $collections = new CollectionsManager();

  // get a collection by it's id
  $sample_collection = $collection->get_collection_by_id( 'sample_collection' );

  // get the elements of the sample collection
  $sample_elements = $collection->get_elements_by_id( 'sample_collection' );

  // insert an alement
  $new_element = array( 'id' => 5, 'name' => 'new_element', 'url' => 'http://wordpress.stackexchange.com' );
  $new_element_id = $collection->insert_element_by_id( 'sample_collection', $new_element );

I wrote this class from scratch and it is not been tested yet. Maybe it's buggy in some parts. I give no warranty for bugless functionality!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.