如何将数组放入字符串中,然后使用 laravel 5 对字符串进行索引
How do you put an array into a string and then index the string using laravel 5
好的,所以我 运行 这个问题我正在尝试拆分/索引我数据库中列中的值。所以我把我一直试图把它放到一个字符串中,我尝试过的所有组合都应该有效,但每次我尝试拆分值时,我都会收到此错误 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 位数字相匹配,并获取所有符合该条件的数字。在此先感谢任何可以提供帮助的人。
眼前的问题类似于这一行:
$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'
这可能不是最佳做法,但我就是这样做的,如果有人知道如何一次索引多个字符,一定要分享哈哈。
@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
好的,所以我 运行 这个问题我正在尝试拆分/索引我数据库中列中的值。所以我把我一直试图把它放到一个字符串中,我尝试过的所有组合都应该有效,但每次我尝试拆分值时,我都会收到此错误 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 位数字相匹配,并获取所有符合该条件的数字。在此先感谢任何可以提供帮助的人。
眼前的问题类似于这一行:
$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'
这可能不是最佳做法,但我就是这样做的,如果有人知道如何一次索引多个字符,一定要分享哈哈。
@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