php Laravel 5.4 数组到字符串转换异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45883251/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:10:05  来源:igfitidea点击:

Laravel 5.4 Array to string conversion exception

phpstringlaravel

提问by Muhammad Muneeb ul haq

I am trying to break down a string into an array and then print the values on the screen. Here is the string that i am trying to break:

我试图将一个字符串分解成一个数组,然后在屏幕上打印这些值。这是我试图打破的字符串:

"Cog|Condor"

“齿轮|秃鹰”

"|" using this to split it. Here is how I am doing it:

“|” 使用它来拆分它。这是我的做法:

<?= $arrays = explode('|', $b->brand); foreach($arrays as $array){echo $array;}  ?> 

But i keep getting this exception:

但我不断收到此异常:

    2/2) ErrorException
Array to string conversion (View: D:\Code\PHP\Code\CrownBillingSystem\resources\views\pages\print.blade.php)
in 6e7ee4930110d4a26a3e31e0ddfe8b87849a1319.php (line 93)
at CompilerEngine->handleViewException(object(ErrorException), 1)
in PhpEngine.php (line 44)
at PhpEngine-

I cannot figure out what is wrong here.

我无法弄清楚这里出了什么问题。

采纳答案by mbozwood

While the other answers are not incorrect, Blade has been designed to eradicate the use of PHP tags. Blade functions allow you to do everything.

虽然其他答案没有错,但 Blade 旨在消除 PHP 标签的使用。Blade 功能让您无所不能。

The error being produced here is that <?=is shorthand for <php echo. So your code will render as echo $arraysin pseudo code terms which is where the PHP is breaking because you cannot echo an array.

这里所产生的误差存在是<?=对速记<php echo。因此,您的代码将以echo $arrays伪代码术语呈现,这是 PHP 中断的地方,因为您无法回显数组。

To better your code in this instance, you should be manipulating as much of the data as possible in the controller which is also mentioned herein the blade documentation.

为了在这种情况下改进您的代码,您应该在控制器中操作尽可能多的数据,刀片文档中也提到这一点。

Might I suggest modifying your code, to produce the same result, but using blade.

我是否建议修改您的代码以产生相同的结果,但使用刀片。

@php 
    $arrays = explode('|', $b->brand); 
@endphp

@foreach($arrays as $array)
    {{ $array }}
@endforeach

The above snippet will produce the same results as intended.

上面的代码片段将产生与预期相同的结果。

An even better way of doing it, and to further your understanding would be to return the view from the controller, and pass in $arrayspredefined. Something like this:

一个更好的方法是从控制器返回视图,并传递$arrays预定义的视图。像这样的东西:

public function echoArrays()
{
    $b = Object::find(1); //or however you get $b
    $arrays = explode('|', $b->brand); 
    return view('view1', compact('arrays');
}

The above will allow you to use the code snippet 2 up, but without the @php ...@endphptags, and just use the @foreach() ... @endforeach

以上将允许您使用代码片段 2,但没有@php ...@endphp标签,只需使用@foreach() ... @endforeach

回答by iainn

You can't put multiple statements in <?= ... ?>blocks - it's short-hand for echo, so your code expands to

您不能在<?= ... ?>块中放置多个语句- 它是 echo 的简写,因此您的代码扩展为

<?php
  echo $arrays = explode('|', $b->brand); // This is what's causing your error

  foreach($arrays as $array){echo $array;}
?>

If you want to perform operations as well as output, you just need to use full PHP tags:

如果你想执行操作和输出,你只需要使用完整的 PHP 标签:

<?php $arrays = explode('|', $b->brand); foreach($arrays as $array){echo $array;}  ?> 

回答by ishegg

You are using the PHP short tag <?=which is equivalent to <?php echo. Thus, it's trying to echothe Array, which you can't. Do it like this:

您正在使用 PHP 短标签<?=,它等效于<?php echo. 因此,它正在尝试echo使用 Array,而您却做不到。像这样做:

<?php $arrays = explode('|', $b->brand); foreach($arrays as $array){echo $array;}  ?> 

回答by Ian Rodrigues

You must replace <?=by this <?php.

你必须用<?=这个替换<?php