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 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)