Skip to content
Advertisement

Pushing a sub array into the same array

I am trying to put content of one array into the same array. Here I have an array $mclass with values such as

Array
(
[0] => stdClass Object
    (
        [room_id] => 1,3,5
        [day] => 1
        [class_teacher] => TEA-2014-2
        [final_exam_date] => 2015-09-21
    )
)

You can see I have room_id index with 1,3,5 value. Now, I want to explode the room_id and get duplicate of same array index data with change of room_id and push into the array. and finally delete the current array index such as [0]. Here I want the final result as.

Array
(
    [0] => stdClass Object
    (
        [room_id] => 1
        [day] => 1
        [class_teacher] => TEA-2014-2
        [final_exam_date] => 2015-09-21
    )
    [1] => stdClass Object
    (
        [room_id] => 3
        [day] => 1
        [class_teacher] => TEA-2014-2
        [final_exam_date] => 2015-09-21
    )
    [2] => stdClass Object
    (
        [room_id] => 5
        [day] => 1
        [class_teacher] => TEA-2014-2
        [final_exam_date] => 2015-09-21
    )
)

Here is my code for the same:

if(count($mclass)>0)
    {
        foreach($mclass as $mclasskey=>$mclass_row)
        {

            /* Room ID Calculation */
            if(isset($mclass[$mclasskey]))
            {
                $temp_room_id = explode(',',$mclass_row->room_id);
            
                if(count($temp_room_id)>1)
                {

                    foreach($temp_room_id as $trkey=>$tr)
                    {
                        if(!in_array($temp_room_id[$trkey], $morning_class_semester))
                        {
                            array_push($morning_class_semester,$temp_room_id[$trkey]);
                        }
                    }

                    if(count($morning_class_semester)>0)
                    {
                        foreach($morning_class_semester as $mcskey=>$mcs)
                        {
                            $index_count = count($new_test);
                            $test[$index_count] = $mclass[$mclasskey];
                            
                            $test[$index_count]->room_id = $morning_class_semester[$mcskey];
                            

                            array_push($new_test,$test[$index_count]);
                        }

                        unset($mclass[$mclasskey]);

                    }

                    
                }
            }

        }
    }

Advertisement

Answer

The code below does what you’re looking for using only arrays. So you’ll have to change the array access operators to -> since you’re accessing an object. I’d do so, but it would break the example, so I’ll leave that up to you.

Code Explained: Loop through array selecting each subarray (object in your case), explode on the $item('room_id') … ($item->room_id in your case) … and create sub arrays, via loop, from that using the data from the original using each key. Remove the original item (which has the combined room_ids) and combine the placeholder and original array.

<?php
//Establish some data to work with
$array = array(
    array(
    "room_id" => "1,3,5",
    "day" => 1,
    "class_teacher" => "TEA-2014-2",
    "final_exam_date" => "2015-09-21",
));

foreach ($array as $key => $item) {
    $placeholder = array();
    $ids = explode(',',$item['room_id']);
    if (count($ids) > 1) {
        foreach ($ids as $id) {
            $push = array(
                'room_id' => $id,
                'day' => $item['day'],
                'class_teacher' => $item['class_teacher'],
                'final_exam_date' => $item['final_exam_date']
            );
            array_push($placeholder, $push);
        }
        $array = array_merge($array, $placeholder);
        unset($array[$key]);
    }
}

var_dump($array);
?>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement