PHP-在看似复杂的数组中用相同的键隔离元素

时间:2019-07-16 18:09:35

标签: php arrays key

我有1个单个变量$input_args,其中似乎包含多个数组(多维?):

  

Array([required] => 1 [html_label_text] =>您的性别是什么?[required_validation_error_message] =>请输入您的性别[html_name] => ee_reg_qstn [356] [17] [html_id] => ee-reg-qstn [默认] => 男性

     

Array([required] => 1 [html_label_text] =>最喜欢的颜色?[required_validation_error_message] =>糟糕!好像缺少了一些东西[html_name] => ee_reg_qstn [356] [12] [html_id] => ee-reg-qstn [默认] => 蓝色

     

Array([required] => 1 [html_label_text] =>您的体重是多少?[required_validation_error_message] =>输入体重[html_name] => ee_reg_qstn [356] [18] [html_id] => ee -reg-qstn [默认] => 144

我想隔离与[default]中的$input_args键相对应的不同值,但似乎无法做到这一点。回波$input_args['default']会产生所有值,即MaleBlue144,但是尝试单独选择这些值似乎是在设置值长度$input_args['default'][0]会产生MB1

我是php新手。预先感谢您的帮助!

编辑: 这是进行var_export

后的确切数组
array ( 'required' => true, 'html_label_text' => 'What is your gender?', 'required_validation_error_message' => 'Please enter your gender', 'html_name' => 'ee_reg_qstn[376][17]', 'html_id' => 'ee_reg_qstn-376-17', 'html_class' => 'ee-reg-qstn ee-reg-qstn-17', 'html_label_id' => 'ee_reg_qstn-376-17-lbl', 'html_label_class' => 'ee-reg-qstn', 'default' => 'Male', )array ( 'required' => true, 'html_label_text' => 'What is your color?', 'required_validation_error_message' => 'Oops! Looks like something is missing', 'html_name' => 'ee_reg_qstn[376][12]', 'html_id' => 'ee_reg_qstn-376-12', 'html_class' => 'ee-reg-qstn ee-reg-qstn-12', 'html_label_id' => 'ee_reg_qstn-376-12-lbl', 'html_label_class' => 'ee-reg-qstn', 'default' => 'Brown', )array ( 'required' => true, 'html_label_text' => 'What is your weight?', 'required_validation_error_message' => 'Enter the weight you will be', 'html_name' => 'ee_reg_qstn[376][18]', 'html_id' => 'ee_reg_qstn-376-18', 'html_class' => 'ee-reg-qstn ee-reg-qstn-18', 'html_label_id' => 'ee_reg_qstn-376-18-lbl', 'html_label_class' => 'ee-reg-qstn', 'default' => '111', )array ( 'required' => true, 'html_label_text' => 'What is you academy name?', 'required_validation_error_message' => 'If none, type "Independent"', 'html_name' => 'ee_reg_qstn[376][13]', 'html_id' => 'ee_reg_qstn-376-13', 'html_class' => 'ee-reg-qstn ee-reg-qstn-13', 'html_label_id' => 'ee_reg_qstn-376-13-lbl', 'html_label_class' => 'ee-reg-qstn', 'validation_strategies' => array ( 0 => EE_Max_Length_Validation_Strategy::__set_state(array( '_max_length' => INF, '_validation_error_message' => 'Input is too long. Maximum number of characters is INF', '_input' => NULL, )), ), 'default' => 'Inception', )array ( 'required' => true, 'html_label_text' => 'What is your  team name?', 'required_validation_error_message' => 'If none, type "Independent"', 'html_name' => 'ee_reg_qstn[376][14]', 'html_id' => 'ee_reg_qstn-376-14', 'html_class' => 'ee-reg-qstn ee-reg-qstn-14', 'html_label_id' => 'ee_reg_qstn-376-14-lbl', 'html_label_class' => 'ee-reg-qstn', 'validation_strategies' => array ( 0 => EE_Max_Length_Validation_Strategy::__set_state(array( '_max_length' => INF, '_validation_error_message' => 'Input is too long. Maximum number of characters is INF', '_input' => NULL, )), ), 'default' => 'VS All Stars', )array ( 'required' => false, 'html_label_text' => 'I don\'t want to be matched with opposite gender', 'required_validation_error_message' => '', 'html_name' => 'ee_reg_qstn[376][15]', 'html_id' => 'ee_reg_qstn-376-15', 'html_class' => 'ee-reg-qstn ee-reg-qstn-15', 'html_label_id' => 'ee_reg_qstn-376-15-lbl', 'html_label_class' => 'ee-reg-qstn', 'default' => array ( ), )

1 个答案:

答案 0 :(得分:0)

如果要使用默认键的所有值,则可以使用array_column并将默认值指定为列键。

print_r(array_column($input_args, "default"));

结果

Array
(
    [0] => ** **Male**
    [1] => ** **Blue**
    [2] => ** **144**
)

如果您可能具有可以构成数组键的唯一数据,则可以指定第三个参数。例如:

array_column($input_args, "default", "html_name")

Demo