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 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件