【问题标题】:How do you put an array into a string and then index the string using laravel 5如何将数组放入字符串中,然后使用 laravel 5 对字符串进行索引
【发布时间】:2015-03-31 16:58:31
【问题描述】:

好的,所以我遇到了这个问题,我试图在我的数据库中拆分/索引列中的值。所以我把我一直试图把它放到一个字符串中,以及所有的组合我尝试过应该可以工作,但是每次我尝试拆分值时,我都会收到此错误Array to string conversion,并且错误显示我的数据库与字符串或什至至少是一列相关。现在抱歉,如果我的代码此时看起来工作量超出了应有的范围,哈哈。那是出于绝望,并试图让它发挥作用。

这是我的控制器:

public function FSC_List($id)
{

    $fsg_find_id = fsgdata::find($id);
    $fsc_list_all = fscdata::all();
    $fsc_find_id = fscdata::find($id);
    $num_match = fscdata::all()->toArray();
    $fsc_num_col = fscdata::lists('fsc_number');
    $newstring = implode("",$fsc_num_col);
    $final = $newstring.str_split(0,2); 

return View('FSC_views.FSC_List', compact('fsg_find_id','fsc_list_all',
        'fsc_find_id', 'num_match', 'fsc_num_col', 'newstring','final'));
}

这是我的看法:

@extends('layout.master')
@section('content')
<div class="figure"><img src="/Content/images/figure2.jpg" /></div>
<div class="main">
    <h2>Search Results for FSG Number: {{ $fsg_find_id->fsg_number }}</h2>
        <ul>

        @foreach($fsc_num_col as $fsc_find_id->fsg_number)
            @if($fsg_find_id->fsg_number == $final);
                <a href='/NSN_list={{ $fsc_find_id->id }}'>{{$fsc_find_id->fsc_number}}</a></br>
            @endif
        @endforeach
    </ul>
</div>
@endsection

请注意,我的目标是让用户选择 2 个数字来匹配数据库中一堆 4 位数字的前 2 个数字,并获取所有符合该条件的数字。提前感谢任何可以提供帮助的人。

【问题讨论】:

    标签: php database split type-conversion laravel-5


    【解决方案1】:

    当前的问题如下所示:

    $final = $newstring.str_split(0,2);
    

    基本上,您正在尝试将字符串 $newstring 与数组 str_split(0, 2) 组合,这就是您收到 Array to string conversion 错误的原因。看一下str_split 的手册条目 - 看起来您并没有正确理解它。

    如果您尝试获取字符串的前两个字符,那么您应该使用substr。比如:

    $full_string = 'abcdefg';
    $first_two = substr($full_string, 0, 2);  # $first_two = 'ab'
    

    【讨论】:

      【解决方案2】:

      这可能不是最佳实践,但我就是这样做的,如果有人知道如何一次索引多个字符,请务必分享哈哈。

      @foreach($fsc_list_all as $fsc_find_id)
          @if($fsg_find_id->fsg_number[0] == $fsc_find_id->fsc_number{0} &&
            $fsg_find_id->fsg_number[1] == $fsc_find_id->fsc_number{1})
      
                <a href='/NSN_list={{ $fsc_find_id->id }}'>{{$fsc_find_id->fsc_number}}</a></br>
      
           @endif
      @endforeach
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多