php - 选定数据的输出数组

标签 php html

我有一个数组,其中包含来自不同形式的数据。我需要一种方法来获取 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/

相关文章:

jquery - 在一行中对齐两个跨度类

html - Struts:选择了 HTML 选项?

php - 使用 end($array) 检索数组中的最后一行

php - Nginx 为静态文件代理不正确的 fastcgi_script_name

javascript - 如何在Jquery中删除Ol子元素没有Li的Li元素?

javascript - Google Chrome 66 自动播放政策变更

html - HTML 中的数学集合符号

php - 如何避免JS代码变成input?

php - HTML 表单打开 PHP 脚本而不是运行它

php - 如何在 php 中对数组中的 JSON 对象进行编码?