【问题标题】:Getting array keys instead of the value with Laravel使用 Laravel 获取数组键而不是值
【发布时间】:2017-07-21 23:16:21
【问题描述】:

我想遍历一个数组并使用 Laravel 刀片引擎显示它时遇到问题

我的控制器代码是这样的

    $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'));

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

<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>

这是我使用以下代码得到的结果:

我想要的结果:

【问题讨论】:

  • $content as $value then {{ put here your array index name }}

标签: php mysql arrays laravel laravel-blade


【解决方案1】:

这样改

<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>

【讨论】:

  • 我知道并且我已经做到了,但我希望用户自动获取来自用户选择的表的列,如果你理解的话
  • 这里的 foreach 循环已经做了你想做的事情......一旦检查了这个
  • 我的意思是,如果表用户有列 id 用户名电子邮件等,我希望动态显示,因为他会在使用不同的列 ex 之后选择另一个表。产品 id 名称 价格 等等 这就是为什么我希望它是动态的
  • 首先通过 print_r 检查 $content 数组值,如果循环有多个值,然后循环自动重复打印 tr 及其值 ...
  • Collection {#196 ▼ #items: array:2 [▼ 0 =&gt; {#194 ▼ +"onomateponimo": "Mukja Eluert" +"email": "e.mukja@icloud.com" +"kinito": "123456789" } 1 =&gt; {#190 ▼ +"onomateponimo": "Eluert " +"email": "e.mukja@outlook.com" +"kinito": "123456789" } ] }
【解决方案2】:

php有一个函数叫array_keys();

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

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

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

【解决方案3】:

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

【讨论】:

    【解决方案4】:

    使用array_keys()从数组中获取所有键,你也可以使用foreach($arr as $key =&gt; $value)

    【讨论】:

      【解决方案5】:

      我找到了答案,我只是从这里改变了我的看法

      <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>
      

      到这里

      <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>
      

      【讨论】:

        【解决方案6】:
        $hayStack = ['name' => 'John', 'age' => 30, 'sex' => 'm'];
        $searchValue = 30;
        $keyName = array_keys($hayStack, $searchValue)[0]; 
        

        array_keys() 将搜索所有可能包含搜索值的键。考虑到$hayStack 是一个数组,不会有任何重复的键,所以我们的结果将在索引 0 处。
        对于搜索值为 30,结果将是“年龄”。 这里array_keys是一个内置函数https://www.php.net/manual/en/function.array-keys.php

        【讨论】:

        • 您好,欢迎来到 SO!请解释您的解决方案并最好添加对文档的引用。我不认为你写的海报能理解它。
        猜你喜欢
        • 2018-08-09
        • 2012-09-19
        • 1970-01-01
        • 2020-01-10
        • 2015-12-16
        • 2017-08-27
        • 1970-01-01
        • 1970-01-01
        • 2019-11-07
        相关资源
        最近更新 更多