2014-09-23 42 views
2

我有一個數組,其中包含來自不同形式的數據。我需要一種方法來獲取form_name =人口統計數據並輸出它們。從所選數據輸出數組

我已經嘗試了while循環地發現,包括FORM_NAME「人口」的陣列,但我得到

是由產生該陣列中的任何結果:

$question_set_p1 = []; 
$num_questions_set_p1 = count($choice_set_p1); 

//get the keys from the choice_set array 
$question_num_p1 = array_keys($choice_set_p1); 

for($i = 0; $i < $num_questions_set_p1; $i++){ 
    $question_set_p1[] = $tyler->prepQuestion($data_dictionary_p1, $choice_set_p1, $question_num_p1, $i); 
} 

在競爭者的陣列:

[1]=> 
    array(11) { 
    ["field_label"]=> 
    string(24) "What is your first name?" 
    ["field_name"]=> 
    string(6) "f_name" 
    ["identifier"]=> 
    bool(true) 
    ["options"]=> 
    bool(false) 
    ["form_name"]=> 
    string(12) "demographics" 
    } 
    [2]=> 
    array(11) { 
    ["field_label"]=> 
    string(23) "What is your last name?" 
    ["field_name"]=> 
    string(6) "l_name" 
    ["identifier"]=> 
    bool(true) 
    ["options"]=> 
    bool(false) 
    ["form_name"]=> 
    string(12) "demographics" 
    } 
[3]=> 
    array(11) { 
    ["field_label"]=> 
    string(32) "Researcher who took measurements" 
    ["field_name"]=> 
    string(17) "weight_researcher" 
    ["identifier"]=> 
    bool(false) 
    ["options"]=> 
    bool(false) 
    ["form_name"]=> 
    string(6) "weight" 
    } 

回答

2

來獲取數據,你可以使用array_filter()

$data = array_filter($data, function($arr){ return $arr['form_name'] === 'demographics'; }); 

至於輸出它,完全取決於你想輸出什麼樣的格式,什麼樣的數據需要顯示等,我真的不能給你一個有價值的演示。無論如何,這很簡單。

+0

正是我需要的! – 2014-09-23 13:53:16

+0

現在,如果我想使用相同的腳本來查找form_name == weight的位置,我無法獲取該數據並顯示它。它的重量是數組中的#3而不是數組中的#1。我怎麼弄到的? – 2014-09-23 14:53:57

+0

不,這是因爲我們用僅限人口統計學的版本覆蓋'$ data'數組。只需將'$ data ='部分更改爲'$ demographicsData =',那麼您可以複製/粘貼該行並將其設置爲'$ weightData =',它應該可以工作:-) – Joe 2014-09-23 14:55:19