重组PHP数组

时间:2014-07-23 19:28:57

标签: php cakephp cakephp-2.0

我尝试了很多不同的变体,但无法获得正确的结构。也许,你的专家可能会尝试一下。

我需要做的是让可能有多个孩子的治疗师归还给他。它将返回分配给他的孩子的日程安排。

我尝试过以下代码。这是我能得到的最接近我需要的格式。

    // Grab the therapist    
    $therapist = $this->Therapist->find('first', array('conditions' => array('Therapist.' . $this->Therapist->primaryKey => $id)));
    // Grab the child(ren) assigned to the therapist
   // Returns id as key and name as value.
    $children =  $this->Child->get_children($id);
    $schedule = array();
   // Loop through the assigned children and get the id (key).
   foreach($children as $key => $value):
        // Loop through and grab all the scheduled days and times for child(ren).
        foreach($this->Schedule->get_full_schedule($key) as $child):
            // Have the child name at the top of the array. 
            if($name != $child['Child']['child_name']):
                 $schedule[] = array_push($schedule,  array('child_name' => $child['Child']['child_name']));
                $name = $child['Child']['child_name'];
            endif;
                // Get all the scheduled days for the child and add to array.
                if($child['Schedule']['child_id'] == $child['Child']['id']):
                    $schedule[]['Schedule'] =  $child['Schedule'];
                endif;
        endforeach;
    endforeach;

哪个输出以下数组:

   Array
                (
                    [0] => Array
                        (
                           [child_name]  => John Smith
                        )

                    [1] => 1
                    [2] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 19
                                    [child_id] => 197
                                    [days] => Monday
                                    [start_time] => 17:00:00
                                    [end_time] => 22:00:00
                                )

                        )

                    [3] => Array
                        (
                            [child_name]  => Jane Smith
                        )

                    [4] => 4
                    [5] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 16
                                    [child_id] => 138
                                    [days] => Monday
                                    [start_time] => 09:00:00
                                    [end_time] => 17:00:00
                                )

                        )

                    [6] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 17
                                    [child_id] => 138
                                    [days] => Sunday
                                    [start_time] => 09:00:00
                                    [end_time] => 12:00:00
                                )

                        )

                    [7] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 18
                                    [child_id] => 138
                                    [days] => Tuesday
                                    [start_time] => 09:00:00
                                    [end_time] => 17:00:00
                                )

                        )

                )

我想要的是:

                Array
                (
                    [0] => Array
                        (
                           [child_name]  => John Smith
                           [0] => Array
                               (
                                [Schedule] => Array
                                    (
                                        [id] => 19
                                        [child_id] => 197
                                        [days] => Monday
                                        [start_time] => 17:00:00
                                        [end_time] => 22:00:00
                                    )
                            )
                        )

                    [1] => Array
                        (
                            [child_name]  => Jane Smith
                            [0] => Array
                                (
                                    [Schedule] => Array
                                        (
                                            [id] => 16
                                            [child_id] => 138
                                            [days] => Monday
                                            [start_time] => 09:00:00
                                            [end_time] => 17:00:00
                                        )

                                )

                            [1] => Array
                                (
                                    [Schedule] => Array
                                        (
                                            [id] => 17
                                            [child_id] => 138
                                            [days] => Sunday
                                            [start_time] => 09:00:00
                                            [end_time] => 12:00:00
                                        )

                                )

                            [2] => Array
                                (
                                    [Schedule] => Array
                                        (
                                            [id] => 18
                                            [child_id] => 138
                                            [days] => Tuesday
                                            [start_time] => 09:00:00
                                            [end_time] => 17:00:00
                                        )

                        )

                )

感谢任何帮助。

感谢, 格雷格

1 个答案:

答案 0 :(得分:1)

您是否考虑过只使用Containable Behavior?看起来比你尝试这样做容易得多:

$this->Therapist->find('first', array(
    'conditions' => array(
        'Therapist.' . $this->Therapist->primaryKey => $id
    ),
    'contain' => array(
        'Child' => array(
            'Schedule'
        )
    )
));

不仅更容易,而且您的数据应以可接受的嵌套格式返回。