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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵