从对象数组(php、laravel blade)中获取所有密钥x
数据库表如下所示:从对象数组(php、laravel blade)中获取所有密钥x,php,html,laravel,laravel-5,eloquent,Php,Html,Laravel,Laravel 5,Eloquent,数据库表如下所示: [ {"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"}, {"id":93,"product_id":8966,"extra_type":"Extras","extra_name":"Ketchup"}, {"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"} ] 数据库中的“extras”
[
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"},
{"id":93,"product_id":8966,"extra_type":"Extras","extra_name":"Ketchup"},
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"}
]
数据库中的“extras”列存储的对象数组如下所示:
[
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"},
{"id":93,"product_id":8966,"extra_type":"Extras","extra_name":"Ketchup"},
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"}
]
下面的代码(来自Laravel blade)返回对象数组
@foreach($item->orderedProducts as $op)
<tr>
<td>
<span>{{ $op['extras'] }}</span><br>
</td>
</tr>
@endforeach
我试图实现的是从“extras:”中获取(例如)“extra_name”的所有值
所需的输出应类似于:
橄榄
番茄酱
Olives@foreach($item->orderedProducts as$op)
@如果('Extras'=$op->extra_类型)
{{$op->extra_name}}
@恩迪夫
@endforeach
@foreach($item->orderedProducts as$op)
@如果('Extras'=$op->extra_类型)
{{$op->extra_name}}
@恩迪夫
@endforeach
您可以在laravel中使用->
访问对象值,请尝试以下操作
@foreach($item->orderedProducts as $op)
<tr>
<td>
<span>{{ $op->extra_name }}</span><br>
</td>
</tr>
@endforeach
@foreach($item->orderedProducts作为$op)
{{$op->extra_name}}
@endforeach
您可以在laravel中使用->
访问对象值,请尝试以下操作
@foreach($item->orderedProducts as $op)
<tr>
<td>
<span>{{ $op->extra_name }}</span><br>
</td>
</tr>
@endforeach
@foreach($item->orderedProducts作为$op)
{{$op->extra_name}}
@endforeach
我想这会很好
@foreach($item->orderedProducts as $op)
<?php $array = stripslashes(json_encode($op->extras)) ?>
<tr>
<td>
<span>
@foreach($array as $value)
{{ $value['extra_name'] }}
@endforeach
</span><br>
</td>
</tr>
@endforeach
@foreach($item->orderedProducts作为$op)
@foreach($array作为$value)
{{$value['extra_name']}
@endforeach
@endforeach
我想这会很好
@foreach($item->orderedProducts as $op)
<?php $array = stripslashes(json_encode($op->extras)) ?>
<tr>
<td>
<span>
@foreach($array as $value)
{{ $value['extra_name'] }}
@endforeach
</span><br>
</td>
</tr>
@endforeach
@foreach($item->orderedProducts作为$op)
@foreach($array作为$value)
{{$value['extra_name']}
@endforeach
@endforeach
我解决了它。我需要的是在迭代json对象数组时使用json\u decode()
。接下来,我提取了所需的属性
@foreach($item->orderedProducts as $op)
<tr>
<td>
@foreach(json_decode($op->extras, true) as $extra)
{{ $extra['extra_name'] }}<br>
@endforeach
</td>
</tr>
@endforeach
@foreach($item->orderedProducts作为$op)
@foreach(json_解码($op->extras,true)为$extra)
{{$extra['extra_name']}}
@endforeach
@endforeach
我解决了它。我需要的是在迭代json对象数组时使用json\u decode()
。接下来,我提取了所需的属性
@foreach($item->orderedProducts as $op)
<tr>
<td>
@foreach(json_decode($op->extras, true) as $extra)
{{ $extra['extra_name'] }}<br>
@endforeach
</td>
</tr>
@endforeach
@foreach($item->orderedProducts作为$op)
@foreach(json_解码($op->extras,true)为$extra)
{{$extra['extra_name']}}
@endforeach
@endforeach
你能试试{{$op->extras}}
吗?我想你需要{$op->extras}
。你能试试{$op->extras}
吗?你需要{$op->extras}
,我猜。错误例外:为foreach()提供的参数无效。你能通过执行=>打印($op)来发布$op打印的内容吗在第一个foreachor中,您可以尝试将$op['extras']替换为第二个foreach中的$op->extras,因为它添加了$op返回的内容。你知道为什么json中有“\”吗?好的,它被当作字符串,是json。无论如何,在第二个foreach之前,您必须将“extras”格式化为json格式。那你就完了。CheerrorRorException:为foreach()提供的参数无效。请在第一个foreach中发布$op打印的内容…通过执行=>print\r($op),或者您可以尝试将$op['extras']替换为$op->extras,在第二个foreach中添加了$op返回的内容。你知道为什么json中有“\”吗?好的,它被当作字符串,是json。无论如何,在第二个foreach之前,您必须将“extras”格式化为json格式。那你就完了。干杯