[英]How to add strings to array key if that made by laravel5 pluck(lists) method?
Yesterday I solved this issue , Thanks Everyone. 昨天我解决了这个问题 ,谢谢大家。
Next problem. 下一个问题。 Not so important but I feel concern, Look this code.
不是那么重要,但我感到担心,看看这段代码。
Controller 调节器
$months = \App\Test::select(\DB::raw('DATE_PART(\'MONTH\', date) AS MONTH'))
->where('date', '<=', 'now()')
->orderBy('date', 'desc')
->pluck('month', 'month');
// this code generate like this.
// Illuminate\Support\Collection Object ( [items:protected] => Array ( [8] => 8 [7] => 7 ) )
View 视图
{{ Form::select('month', $months, old('month'), ['id' => 'month']) }}
( now generate this. )
<select id="month" name="month">
<option value="8">8</option>
<option value="7">7</option>
</select>
I hope to add string to key like this 我希望像这样添加字符串到键
<select id="month" name="month">
<option value="8">8month</option>
<option value="7">7month</option>
</select>
I think that can do with foreach like this. 我认为这可以像foreach这样做。
$array = ["8" => "8", "7" => "7"];
print_r($array); // Array ( [8] => 8 [7] => 7 )
foreach($array as $key => $value){
$array[$key.'month'] = $value;
unset($array[$key]);
}
print_r($array); // well done! Array ( [8month] => 8 [7month] => 7 )
So test it but... 所以测试一下,但......
print_r($months); // Illuminate\Support\Collection Object ( [items:protected] => Array ( [8] => 8 [7] => 7 ) )
foreach($months as $key => $value){
$array[$key.'month'] = $value;
unset($months[$array]);
}
print_r($months); // Not Working WTF!! Illuminate\Support\Collection Object ( [items:protected] => Array ( ) )
Any Solves? 任何解决?
Your $months
variable is Collection
instance. 您的
$months
变量是Collection
实例。 You can use $months->put($key, $value)
or $months->push($value)
Check out colllection methods here 您可以使用
$months->put($key, $value)
或$months->push($value)
在这里查看colllection方法
Edit: 编辑:
Also, I noticed you used wrong variable in second example. 另外,我注意到你在第二个例子中使用了错误的变量。 Shouldn't it be like this?
不应该这样吗?
foreach($months as $key => $value){
$months[$key.'month'] = $value;
unset($months[$key]);
}
that mistake is the answer lmao XD truly work this, forgive my stupid. 那个错误就是答案lmao XD真正做到这一点,原谅我的愚蠢。
foreach($months as $key => $value){
$months[$key.'month'] = $value;
unset($months[$key]);
}
PS PS
Above Code is MISTAKE 以上代码是MISTAKE
This Code is true. 本准则是真实的。
foreach($months as $key => $value){
$months[$value] = $value.'month';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.