dougou5844 2017-04-23 01:34
浏览 64
已采纳

在PHP中获取多维数组的索引名称

I have a form with multiple input fields, which are serialized and passed to a function via AJAX. Inside the function, I have the data in below form. I need to get Book-Keeper, publish-companies, and 1 as separate variables.

// $key = '_data'
// $values = {"name":"_data[Book-Keeper][publish_companies]","value":"1"}

Here's the function code.

function fnc_edit_roles(WP_REST_Request $request) {

    $variables = $_REQUEST; 

    // Get all $_POST variables into $data[] array with key=>value
    if( $variables ) {

        while ( list( $field, $value ) = each( $_REQUEST )) {

            if( $field !== '_nonce' ) {
                $data[$field] = $value;
            }

        } // end while

    } // end if

    foreach( $data as $key=>$values ) {
        // $key = '_data'
        // $values = {"name":"_data[Book-Keeper][publish_companies]","value":"1"}
        // How do I get 'Book-Keeper', 'publish_companies', and '1' into separate variables?

    }

    $result = array( 'msg' => $data, 'error' => false );
    return json_encode( $result );

}

And here's how I get the inputs in HTML.

<form id="_user_roles_form" method="post" class="form-horizontal">
    <div class="form-group row">
        <label class="col-lg-6 control-label">Publish Companies</label>
        <div class="col-lg-6">
            <div class="btn-group" data-toggle="buttons">
                <label class="btn btn-default active">
                    <input type="radio" name="_data[Book-Keeper][publish_companies]" value="1"/> True
                </label>
                <label class="btn btn-default ">
                    <input type="radio" name="_data[Book-Keeper][publish_companies]" value="0"/> False
                </label>
            </div>
        </div>
    </div>
</form>

And here's how I pass the inputted data to AJAX function.

var _data = jQuery('#_user_roles_form').serializeArray();

jQuery.ajax({
    url: myAjaxUrl,
    type: 'POST',
    data: _data,
    success: function( json ) {
        var result = JSON.parse( json );
        console.log( result.msg );
    }
});

Long question short, how do I get Book-Keeper and publish_companies out of the string _data[Book-Keeper][publish_companies]? key or array_keys doesn't work here...

  • 写回答

1条回答 默认 最新

  • duanrang9348 2017-04-23 02:22
    关注

    Try this hope this will help you out. Here we are using Regex: \[([\w\-]+)\] which will match Example: [something-some_value]

    Try this code snippet here

    ini_set('display_errors', 1);
    
    foreach ($data as $key => $values)
    {
        //$key = '_data';
        //$values = '{"name":"_data[Book-Keeper][publish_companies]","value":"1"}';
        $array = json_decode($values, true);
        $value = $array["value"];//will contain value
        preg_match_all("/\[([\w\-]+)\]/", $array["name"], $matches);
        list($role_name,$cap_name)=$matches[1];
        echo $role_name;//will contain role name
        echo $cap_name;//will contain company name
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?