普通网友 2019-07-16 18:09
浏览 81
已采纳

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

I have 1 single variable $input_args which seems to contain multiple arrays (multidimensional?):

Array ( [required] => 1 [html_label_text] => What is your gender? [required_validation_error_message] => Please enter your gender [html_name] => ee_reg_qstn[356][17] [html_id] => ee-reg-qstn [default] => Male )

Array ( [required] => 1 [html_label_text] => Favorite color? [required_validation_error_message] => Oops! Looks like something is missing [html_name] => ee_reg_qstn[356][12] [html_id] => ee-reg-qstn [default] => Blue )

Array ( [required] => 1 [html_label_text] => What is your weight? [required_validation_error_message] => Enter your weight [html_name] => ee_reg_qstn[356][18] [html_id] => ee-reg-qstn [default] => 144 )

I want to isolate the different values corresponding to the [default] key in $input_args but I cannot seem to do it. Echoing $input_args['default'] yields all the values i.e. MaleBlue144, but try to select the values individually seem to be setting the value lengths $input_args['default'][0] yields MB1.

I am a php novice. Thanks in advance for helping!

edit: this is the exact array(s) upon doing 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条回答 默认 最新

  • dougang5088 2019-07-16 18:32
    关注

    If you want all the values from the default key, you might use array_column and specify default as the column key.

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

    Result

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

    If you perhaps have unique data that can form an array key, you might specify a third parameter. For example:

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

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)