Laravel 从数据库中分解数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17096978/
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
Laravel Explode Array from Database
提问by Rachael
In my database i have a text field of
在我的数据库中,我有一个文本字段
"6:00 AM Registration Opens; Day of Race registration (if available), check-in, packet pick up. 7:30 AM - First Wave Start. 10:00 AM - Awards Ceremony (time is approximate)."
What I am trying to do is have it break everywhere there is a .
我正在尝试做的是让它在任何有 .
@foreach($eventDetails as $info)
<p>{{explode('.', $info->eventSchedule)}}</p>
@endforeach
The error I keep getting is
我不断收到的错误是
"Array to string conversion"
采纳答案by Lawrence Cherone
Try str_replace() instead:
尝试 str_replace() 代替:
<p>{{str_replace('.','.<br>', $info->eventSchedule)}}</p>
<p>{{str_replace('.','.<br>', $info->eventSchedule)}}</p>
回答by Sam
explode('.', $info->eventSchedule)
returns an array() of strings. In blade templating engine (which Laravel uses), anything in double brackets {{ 'Hello world' }}
is converted to and echo statement <?php echo 'Hello World'; ?>
.
explode('.', $info->eventSchedule)
返回一个字符串数组()。在刀片模板引擎(Laravel 使用的)中,双括号中的任何内容{{ 'Hello world' }}
都转换为 and echo statement <?php echo 'Hello World'; ?>
。
You cannot echo an array, so <?php echo explode('.', $info->eventSchedule); ?>
fails. I'm not sure exactly your goal, but I would try this:
您无法回显数组,因此<?php echo explode('.', $info->eventSchedule); ?>
失败。我不确定你的目标,但我会试试这个:
@foreach($eventDetails as $info)
@foreach(explode('.', $info->eventSchedule) as $string)
{{ $string }}
@endforeach
@endforeach
This will now loop through the array created by explode()
, and echo the String through blade's templating engine.
这现在将遍历由创建的数组explode()
,并通过刀片的模板引擎回显字符串。