0

Essentially, I am trying to format the follow:

I have a series of users, that are part of different communities and each community has a series of tasks to complete BUT some of the tasks within the community are only related to some users. Like so:

Community 1
Members: Joe, James 

Tasks: Task 1, Task 2, Task 3

Assigned: Task 1 -> Joe, Task 3 -> James 

Community 2:
Members: James

Tasks: Talk 1, Task 14, Task 15 

Assigned: Task 1 -> Joe, Task 14 -> James 

So essentially. Joe has to complete Task 1, and James has to complete Task 3.

I need a array (that can be encoded to json) that stores the community ID as well as all of the tasks that they have completed but, it should be easily accessible to get the community and the tasks that they have completed.

I have a list of communities that currently exist, and I would like to show all of tasks that have been completed (by the specific user) depending on the community id, as well as this, I also want to add and delete things from the "tasks" category, so need a way to easily get access to these members

I have come up with the following so far:

$progress = array (

    "communities" => array(
        "id" => 1, 
        "tasks" => array(
            1 => "completed",
            2 => "completed"
        ),

        "id" => 2,
        "tasks" => array(
            150 => "completed",
            140 => "completed"
        )
    ),
);

But I don't know if this is the right style of array for this, since, I don't know how complex it will be when I need to add/remove or show the total amount of tasks left for a communities

UPDATE:

This array I'm working with now:

$x = array(

1 => array(
    1, 
    2, 
    3, 
    4,
),

2 => array(
    3,
    5, 
    6,
    10
)

);

Then produces this kind of JSON:

{"1":[1,2,3,4],"2":[3,5,6,10]}

Is this somewhere right? Will I be able to add and delete nodes, as well as add top layer sections to this?

6
  • First off, id and tasks column will get overwritten with the last value. You need to encapsulate the id and tasks into a separate array Aug 7, 2015 at 13:09
  • @AlexTartan Please see my updates, is this kind of heading into the right direction?
    – Phorce
    Aug 7, 2015 at 13:20
  • 1
    i'd go with an associative array (easier to know what's what), but yeah, that might work. Just out of curiosity, aren't you getting these from a database? and why do you need JSON? Aug 7, 2015 at 13:50
  • @AlexTartan I'll take a look at an associate array. Also, there are a lot of communities, communities/tasks are not linked directly to the user.. Running multiple queries will slow the process time down. I'm storing it as a JSON string so then I can fetch it down from the users table and don't really need to query it again
    – Phorce
    Aug 7, 2015 at 13:54
  • I would recommend the use of associative array, for a better readability. Also think about futures evolutions, what if you you need to get the user who works on a specific task? Then you'll just have to expand the task part like "communities" => array("id" => 1, "tasks" => array(1 => "completed", "assigned user" => 1... Aug 7, 2015 at 14:12

1 Answer 1

1

Code sample as a reply to your comment

$progress = array (
    "communities" => array(
        1 => array(
            "tasks" => array(
                1 => array("status" => "completed", "assigned_users" => array("James", "Joe")), 
                2 => array("status" => "pending", "assigned_users" => array("James"))
            ),
        ),
        2 => array(
            //Content of task2 
    )
    )
);

With an extended model like this, you'll be able to:

  • assign a task to one user or more
  • remove a user from a specific task
  • know which users are busy or available
  • count tasks and getting resources (users) assigned

Getting the task 1 of the first community

echo $progress['communities'][1]['tasks'][1];

Walk the communities collection

foreach ($progress['communities'] as $c) {
    //Browse the tasks
    foreach ($c['tasks'] as $t) {
        var_dump($t);
    }
}
3
  • This looks interesting. I'll have a look more when I get back! Adding / Deleting / Viewing, is that simple? So I can get all of the tasks from communities where id = 1?
    – Phorce
    Aug 7, 2015 at 14:51
  • I've just had a play around with this, and it looks pefect for what I want to do! The only thing is, how would I then return the values that are related to id = 1 or id = 2 if there were 2 elements inside this array?
    – Phorce
    Aug 7, 2015 at 17:44
  • Updated my code, id of communities are now used as keys Aug 8, 2015 at 19:47

Your Answer

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

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