使用 Laravel 获取数组键而不是值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42551877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:27:15  来源:igfitidea点击:

Getting array keys instead of the value with laravel

phpmysqlarrayslaravelblade

提问by Eth0

Hello everyone im getting a problem while i want to loop throug an array and display it with laravel blade engine

大家好,我想循环遍历数组并使用 laravel 刀片引擎显示它时遇到问题

My controllers code is like this

我的控制器代码是这样的

    $table =  DB::table('tables')->select('id')->where('name', strtolower($name))->first();

    $columns =  Column::where('table_id', $table->id)->get();

    foreach ($columns as $col) {
        $data[] = $col->col_name;
    };

    $content = DB::table($name)->select(...$data)->get();


    return view('back.group.view-table', compact('content', 'columns', 'data'));

And my blade view code is like this

我的刀片视图代码是这样的

<table class="table table-striped">
            <thead>
                <tr>
                    @foreach($columns as $column)
                        <th>{{ $column->dis_name }}</th>
                    @endforeach
                </tr>
            </thead>
            <tbody>
                @foreach ($content as  $value)
                    <tr>

                        @for ($i = 0; $i < count($columns); $i++)
                            <td>{{ $value->key = $data[$i] }}</td>
                        @endfor

                    </tr>
                @endforeach

            </tbody>
        </table>

This is the result wich i get with the following code This is the result wich i get with the following code

这是我使用以下代码得到的结果 This is the result wich i get with the following code

The result i want to have The result i want to have

我想要的结果 The result i want to have

回答by Nicolas

php has a function called array_keys();

php 有一个名为array_keys()的函数;

so it looks something like this I think

所以我认为它看起来像这样

$results = DB::table($name)->select(...$data)->get();
$content = $results->first();
$keys = array_keys($content->toArray());

this will get you the keys. If you need the keys from each result just loop over $results and for each $result get the keys, using the array_keys syntax.

这会给你钥匙。如果您需要每个结果的键,只需循环 $results 并为每个 $result 获取键,使用 array_keys 语法。

回答by online Thomas

Another solution would be using array_flip()it will flip the keys and values around.

另一种解决方案是使用array_flip()它将翻转键和值。

回答by Kris Roofe

use array_keys()to get all the keys from an array, you also can use foreach($arr as $key => $value)

用于array_keys()从数组中获取所有键,您也可以使用foreach($arr as $key => $value)

回答by Eth0

I Found the answer i just change my view from this

我找到了答案我只是改变了我的看法

<table class="table table-striped">
        <thead>
            <tr>
                @foreach($columns as $column)
                    <th>{{ $column->dis_name }}</th>
                @endforeach
            </tr>
        </thead>
        <tbody>
            @foreach ($content as  $value)
                <tr>

                    @for ($i = 0; $i < count($columns); $i++)
                        <td>{{ $value->key = $data[$i] }}</td>
                    @endfor

                </tr>
            @endforeach

        </tbody>
    </table>

To this

对此

<table class="table table-striped">
            <thead>
                <tr>
                    @foreach($columns as $column)
                        <th>{{ $column->dis_name }}</th>
                    @endforeach
                </tr>
            </thead>
            <tbody>
                @foreach ($content as  $value)
                    <tr>

                        @foreach ($data as $key)
                            <td>{{ $value->$key }}</td>
                        @endforeach

                    </tr>
                @endforeach

            </tbody>
        </table>

回答by Aman Kumar

Change like this

改成这样

<table class="table table-striped">
     <thead>
       <tr>
        @foreach($columns as $column)
          <th>{{ $column->dis_name }}</th>
        @endforeach
        </tr>
      </thead>
      <tbody>
      @foreach ($content as  $value)
      <tr>
         <td>{{ $value->name }}</td>
      </tr>
      @endforeach

     </tbody>
</table>