Welcome Guest, Not a member yet? Register   Sign In
need help with array
#1

[eluser]UnknownPlayer[/eluser]
Hi, i need help with array, this is my array structure:
Code:
Array
(
    [0] => Array
        (
            [id] => 1
            [thread_id] => 1
            [body] => text text text
            [priority] => 2
            [sender_id] => 1
            [cdate] => 2012-07-27 17:01:28
            [status] => 1
            [subject] => subj
            [username] => up
        )

    [1] => Array
        (
            [id] => 31
            [thread_id] => 1
            [body] => 213
            [priority] => 2
            [sender_id] => 26
            [cdate] => 2012-07-27 19:46:39
            [status] => 0
            [subject] => subj
            [username] => up
        )

    [2] => Array
        (
            [id] => 32
            [thread_id] => 1
            [body] => 213
            [priority] => 2
            [sender_id] => 26
            [cdate] => 2012-07-27 19:46:40
            [status] => 0
            [subject] => subj
            [username] => up
        )

    [3] => Array
        (
            [id] => 33
            [thread_id] => 2
            [body] => 213
            [priority] => 2
            [sender_id] => 26
            [cdate] => 2012-07-27 19:46:41
            [status] => 0
            [subject] => subj
            [username] => up
        )

    [4] => Array
        (
            [id] => 34
            [thread_id] => 2
            [body] => 213
            [priority] => 2
            [sender_id] => 26
            [cdate] => 2012-07-27 19:46:41
            [status] => 0
            [subject] => subj
            [username] => up
        )


there is my controller:

Code:
public function all() {
  $user_id = '1';
  $msgs = $this->Msg_model->get_all_threads($user_id);

  foreach ($msgs as $msg) {
   // need code here
  }
}


now i need to array be grouped by threads, in thread_id = 1 to be in array set all messages that belog to that thread(in this case first and second), and on thread_id = 2 to be third and fourth, can someone help me ?
#2

[eluser]_druu[/eluser]
Probably something like this:

Code:
public function all() {
  $user_id = '1';
  $msgs = $this->Msg_model->get_all_threads($user_id);
  
  // Let's have a new Array
  $threads = array();

  foreach ($msgs as $msg) {
    // need code here
    if (isset($threads[$msg['thread_id']]))
    {
      $threads[$msg['thread_id']] = array($msg);
    }
    else
    {
      $threads[$msg['thread_id']][] = $msg;
    }
  }
}
#3

[eluser]UnknownPlayer[/eluser]
Now it shows me only 1 message by thread, it doesnt show me 2 for thred_id 1 and 2 for thred_id 2, only 1 on both Sad
#4

[eluser]_druu[/eluser]
OH of course it should be:

Code:
if (!isset($threads[$msg['thread_id']]))
#5

[eluser]UnknownPlayer[/eluser]
Thanks so much, that is what i need.

I modified this code, becouse i wonna have thread_id and messages field where are mesages:
Code:
public function all() {
  $user_id = '1';
  $msgs = $this->Msg_model->get_all_threads($user_id);
  
  // Let's have a new Array
  $threads = array();

  foreach ($msgs as $msg) {
    // need code here

    if (isset($threads[$msg['thread_id']]))
    {
      $threads[$msg['thread_id']]['thread_id'] = $msg['thread_id']; // added thread_id array field
      $threads[$msg['thread_id']]['messages'] = array($msg); // added messages array field
    }
    else
    {
      $threads[$msg['thread_id']]['messages'][] = $msg; // added messages array field
    }
  }
}


Now array looks:

Code:
Array
(
    [1] => Array
        (
            [thread_id] => 1
            [posts] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [thread_id] => 1
                            [body] => text text text
                            [priority] => 2
                            [sender_id] => 1
                            [cdate] => 2012-07-27 17:01:28
                            [status] => 1
                            [subject] => subj
                            [username] => UnknownPlayer
                        )

                    [1] => Array
                        (
                            [id] => 31
                            [thread_id] => 1
                            [body] => 213
                            [priority] => 2
                            [sender_id] => 26
                            [cdate] => 2012-07-27 19:46:39
                            [status] => 0
                            [subject] => subj
                            [username] => up
                        )

                )

        )

    [2] => Array
        (
            [thread_id] => 2
            [posts] => Array
                (
                    [0] => Array
                        (
                            [id] => 37
                            [thread_id] => 2
                            [body] => aaaaa
                            [priority] => 2
                            [sender_id] => 1
                            [cdate] => 2012-07-27 20:11:55
                            [status] => 1
                            [subject] =>
                            [username] => UnknownPlayer
                        )
                    [1] => Array
                        (
                            [id] => 37
                            [thread_id] => 2
                            [body] => aaaaa
                            [priority] => 2
                            [sender_id] => 1
                            [cdate] => 2012-07-27 20:11:55
                            [status] => 1
                            [subject] =>
                            [username] => UnknownPlayer
                        )


                )

        )

Correct me if i am wrong somewhere please ?
Thanks again.
#6

[eluser]_druu[/eluser]
Well it's perfectly okay IMHO.
But a bit overkill too I'd say.

Cuz with my solution you could do
Code:
foreach ($threads as $thread_id => $post) { /* do stuff here */ }

or
Code:
$posts = $threads[$wanted_thread_id];

so not sure if there's any benefit in adding another array layer.
#7

[eluser]UnknownPlayer[/eluser]
Can you post me code for your solution please ?
#8

[eluser]_druu[/eluser]
Well, first tell me what you want to do with your data.

Do you want to show a list of all posts grouped by threads?
Do you want a complete post list?

What are you trying to do?
#9

[eluser]UnknownPlayer[/eluser]
Grouped by thread_id but like this:

Code:
Array
(
    [1] => Array
        (
            [thread_id] => 1
            [messages] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [thread_id] => 1
                            [body] => text text text
                            [priority] => 2
                            [sender_id] => 1
                            [cdate] => 2012-07-27 17:01:28
                            [status] => 1
                            [subject] => subj
                            [username] => UnknownPlayer
                        )

                    [1] => Array
                        (
                            [id] => 31
                            [thread_id] => 1
                            [body] => 213
                            [priority] => 2
                            [sender_id] => 26
                            [cdate] => 2012-07-27 19:46:39
                            [status] => 0
                            [subject] => subj
                            [username] => up
                        )

                )

        )

    [2] => Array
        (
            [thread_id] => 2
            [messages] => Array
                (
                    [0] => Array
                        (
                            [id] => 37
                            [thread_id] => 2
                            [body] => aaaaa
                            [priority] => 2
                            [sender_id] => 1
                            [cdate] => 2012-07-27 20:11:55
                            [status] => 1
                            [subject] =>
                            [username] => UnknownPlayer
                        )

                )

        )

you see, there is thred_id and messages field, and there are posts for that thread_id.
#10

[eluser]_druu[/eluser]
What I don't get is: What do you think is your benefit of adding extra array layer?
I still don't get it.




Theme © iAndrew 2016 - Forum software by © MyBB