我有一个数组,其中包含来自不同形式的数据。我需要一种方法来获取 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"
}
最佳答案
要抓取数据,您可以使用 array_filter()
:
$data = array_filter($data, function($arr){ return $arr['form_name'] === 'demographics'; });
至于输出它,这完全取决于您希望以什么格式输出、需要显示什么数据等等,我无法真正为您提供有值(value)的演示。无论如何,这非常简单。
关于php - 选定数据的输出数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25996799/