2014-03-24 59 views
-2

我一個以下數組標題$ grid_data如下:如何在PHP中的現有密鑰下創建數組?

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 
         ) 

       ) 

     ) 
) 

現在忽略陣列的其餘部分和只集中在與上述陣列是兩個關鍵要素如下:

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

實際上,我想將這個逗號分隔的字符串轉換爲數組中存在的每個數組元素的關鍵字[que_issue]下的數組。我想在上述陣列($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 
    ) 

現在誰能幫助我在爲了具有對於每個陣列元素存在於它上面的輸出操縱陣列$grid_data?提前致謝。

+1

'$ grid_data [0] ['que_issue'] = explode(',',$ grid_data [0] ['que_issue'])' –

+0

@RoyalBg:我知道但我該如何申請存在於標題爲$ grid_data的數組? – PHPLover

+3

當你不熟悉'foreach()'或'for()':)時,你是什麼樣的PHP愛好者)) –

回答

2

足夠容易foreachexplode

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

或者:

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

我可以擁有不使用引用的代碼嗎? – PHPLover

+0

是的,你可以:) –

+0

@PHPLover:當然,爲什麼不使用可用的? – AbraCadaver

1

不使用參考:

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

foreach()使用$key