Escape ] 字符串中的字符

Escape ] Character in String

我有一个嵌套循环,用于生成选项表单元素。我试图让我的输出是

round_1 [
  'playername' => 'id'
]

我相信我现在的代码是胡说八道,因为在将括号传递到 <select name=... 语句时,括号没有被转义。其他一切正常。

有没有办法转义 $player 并将其传递到 PHP 或 Blade 中的数组?

我已经粘贴了我的代码的相关块:

{!! Form::open(['url' => 'admin/games/process']) !!}

@foreach ($players_array as $game)
    <h4>Round {{$round}}</h4>
    <div class="form-group">

        @foreach($game as $player)
            <p><strong>{{ $player }}</strong></p>

            <!-- This chunk might be able to be written cleaner using laravelcollective/html -->
            <select name="round_{{$round}}[{{$player}}]">
                @foreach($all_players as $key=>$p)
                    <option value="{{ $key }}">{{ $p }}</option>
                @endforeach
            </select>

        @endforeach
    </div>
    <!-- {{ $round++ }} -->

@endforeach

<div class="form-group">
    {!! Form::submit('Submit Refactored Players', ['class' => 'btn btn-outline-primary form-control']) !!}
</div>
{!! Form::close() !!}

谢谢!

解决方案是使用str_replace替换]字符,将其传递给控制器​​,然后创建一个新数组以反向替换旧数组,因为您无法可靠地更改php 键。

查看

@foreach ($players_array as $game)
    <h4>Round {{$round}}</h4>
    <div class="form-group">

        @foreach($game as $player)
            <p><strong>{{ $player }}</strong></p>

            <!-- This chunk might be able to be written cleaner using laravelcollective/html -->
            <select name="round_{{$round}}[{{str_replace("]", "fstupidkey", $player)}}]">
                @foreach($all_players as $key=>$p)
                    <option value="{{ $key }}">{{ $p }}</option>
                @endforeach
            </select>

        @endforeach
    </div>
    <!-- {{ $round++ }} -->

@endforeach

控制器

public function process_logs(Request $request)
{
    /*
     * Accept player match arrays from user form
     * Input event data into database
     */


    $a = $request->round_1;

    /*
    * Create new array to put shit in..
    */
    $b = [];

    foreach($a as $key=>$item)
    {
        $c = str_replace("fstupidkey", "]", $key);
        $b[$c] = $item;
    }

    dd($b);

}