Question

I've a following array titled $grid_data as follows:

    Array
(
    [0] => Array
        (
            [question_id] => 50147
            [question_parent_id] => 0
            [question_subject_id] => 4
            [question_topic_id] => 219
            [question_directions] => 
            [question_text] => A wooden scale of length L and mass M is lying on a frictionless table. A particle of mass m moving the velocity v, at a distanced from the centre as shown in Fig. 93 strikes the scale and adherses to it. Which of the follwoing quantity (quantities) is (are) conserved for the system.


            [question_solution] => 
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 2
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_has_solution] => 0
            [question_site_id] => 
            [question_created_staff_id] => 4e4078987c90dbf4ee477f480ad1b874
            [question_added_date] => 1329728799
            [question_updated_staff_id] => 1096ab29ecde5cec198bb2ebe730d229
            [question_updated_date] => 1340253386
            [que_issue_id] => 1
            [reported_site_id] => ENTPRM
            [reported_user_type] => student
            [reported_user_id] => 395599c5891c1418357e2efa89bc3e27
            [que_issue] => Question is wrong,Other
            [que_issue_comment] => Happy question
            [que_issue_status] => 0
            [que_issue_date] => 1394517725
            [que_issue_fixed_date] => 0
            [question_lock] => 1
            [ques_ans] => Array
                (
                    [0] => Array
                        (
                            [answer_id] => 198739
                            [answer_question_id] => 50147
                            [answer_text] => linear momentum only
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 0
                        )

                    [1] => Array
                        (
                            [answer_id] => 198740
                            [answer_question_id] => 50147
                            [answer_text] => angular momentum only
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 0
                        )

                    [2] => Array
                        (
                            [answer_id] => 198741
                            [answer_question_id] => 50147
                            [answer_text] => kinetic energy only
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 0
                        )

                    [3] => Array
                        (
                            [answer_id] => 198742
                            [answer_question_id] => 50147
                            [answer_text] => linear momentum and angular momentum both
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 1
                        )

                )

        )

    [1] => Array
        (
            [question_id] => 21679
            [question_parent_id] => 0
            [question_subject_id] => 5
            [question_topic_id] => 285
            [question_directions] => 
            [question_text] => In an electrical cable there is a single wire of radius 9 mm of copper. Its resistance is �math xmlns=�http://www.w3.org/1998/Math/MathML���mn�5�/mn��mo��nbsp;�/mo��mi��#937;�/mi��/math�. The cable is replaced by 6 different insulated copper wires, the radius of each wire is 3mm. Now the total resistance of the cable will be
            [question_solution] => 
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 2
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_has_solution] => 0
            [question_site_id] => 
            [question_created_staff_id] => fbfee12504bf3c4a038d4c9f142f894e
            [question_added_date] => 1326440661
            [question_updated_staff_id] => 1096ab29ecde5cec198bb2ebe730d229
            [question_updated_date] => 1338368619
            [que_issue_id] => 2
            [reported_site_id] => ENTPRM
            [reported_user_type] => staff
            [reported_user_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [que_issue] => Question is Incomplete/Wrong,Directions Missing
            [que_issue_comment] => 
            [que_issue_status] => 0
            [que_issue_date] => 1395655234
            [que_issue_fixed_date] => 0
            [question_lock] => 1
            [ques_ans] => Array
                (
                    [0] => Array
                        (
                            [answer_id] => 573122
                            [answer_question_id] => 21679
                            [answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»7«/mn»«mo».«/mo»«mn»5«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 1
                        )

                    [1] => Array
                        (
                            [answer_id] => 573123
                            [answer_question_id] => 21679
                            [answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»45«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 0
                        )

                    [2] => Array
                        (
                            [answer_id] => 573124
                            [answer_question_id] => 21679
                            [answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»90«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 0
                        )

                    [3] => Array
                        (
                            [answer_id] => 573125
                            [answer_question_id] => 21679
                            [answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»270«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
                            [answer_file] => 
                            [answer_description] => 
                            [answer_is_right] => 0
                        )

                )

        )
)

Now ignore rest of the array and concentrate only on two key elements from the above array which are as follows:

[que_issue] => Question is wrong,Other //From first array
[que_issue] => Question is Incomplete/Wrong,Directions Missing //From second array

Actually I want to convert this comma separated strin into an array under the key [que_issue] for every array element present in an array. I want the output in following manner in above array($grid_data):

/*Oputput for first array element from array `$grid_data`*/ 
[que_issue] => Array(
[0] => Question is wrong
[1] =>Other
)
/*Oputput for second array element from array `$grid_data`*/
[que_issue] => Array(
    [0] =>  Question is Incomplete/Wrong
    [1] =>  Directions Missing
    )

Now can anyone help me in manipulating the array $grid_data in order to have the above output for each array element present in it? Thanks in advance.

Was it helpful?

Solution

Easy enough to foreach and explode:

foreach($grid_data as &$array) {
    $array['que_issue'] = explode(',', $array['que_issue']);
}

Or:

foreach($grid_data as $key => $array) {
    $grid_data[$key]['que_issue'] = explode(',', $array['que_issue']);
}

OTHER TIPS

Without using reference:

for($i = 0; $i < count($grid_data); $i++) {
    $grid_data[$i]['que_issue'] = explode(',', $grid_data[$i]['que_issue']);
}

or foreach() using $key

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top