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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!