我试图在@foreach中使用多个数组。看了之后,我发现这不是@foreach的用途所以我应该在以下场景中使用什么?这是我用来模拟网站的虚假数据,然后再将其交给后端开发人员。我正在使用Laravel并且是PHP的初学者,所以我很怜悯。
阵列
$messageData['conversation_sender'][0][0] = 'Clint Smith';
$messageData['conversation_sender'][0][1] = 'Tom Corwin';
$messageData['conversation_sender'][0][2] = 'John Michaels';
$messageData['conversation_sender'][0][3] = 'Jane Winnipeg';
$messageData['conversation_class'][0][0] = 'owner-comment';
$messageData['conversation_class'][0][1] = 'friend-comment';
$messageData['conversation_class'][0][2] = 'friend-comment';
$messageData['conversation_class'][0][3] = 'friend-comment';
$messageData['conversation_message'][0][0] = 'Hello!';
$messageData['conversation_message'][0][1] = 'Can\t wait to catch up';
$messageData['conversation_message'][0][2] = 'Whoop, whoop';
$messageData['conversation_message'][0][3] = 'Good times!';
$messageData['conversation_time'][0][0] = '2 minutes ago';
$messageData['conversation_time'][0][1] = '4 minutes ago';
$messageData['conversation_time'][0][2] = '8 minutes ago';
$messageData['conversation_time'][0][3] = '18 minutes ago';
我一直在使用的@foreach循环示例,一次一个数组
@foreach($messageData['conversation_sender'][0] as $value)
<span class="form-tag">{{ $value }} <i class="fa fa-plus"></i></span>
@endforeach
我是如何使用它的(我知道我在做梦)
@for($messageData['conversation_sender'][0] as $sender, $messageData['conversation_class'][0] as $class, $messageData['conversation_message'][0] as $message, $messageData['conversation_time'][0] as $time)
<li class="{{ $class }} clearfix">
<p class="text-left">
<strong>{{ $sender }}</strong><br>
{{ $message }}
</p>
<span class="message-created text-muted">{{ $time }}</span>
</li>
@endfor
第一次迭代的打印示例
<li class="owner-comment clearfix">
<p class="text-left">
<strong>Clint Smith</strong><br>
Hello!
</p>
<span class="message-created text-muted">2 minutes ago</span>
</li>
答案 0 :(得分:2)
将数组重组为应该是的:层次结构。
$messageData[] = array(
'conversation_sender' => 'Clint Smith',
'conversation_class' => 'owner-comment',
'conversation_message' => 'Hello!',
'conversation_time' => '2 minutes ago'
);
// Repeat for each other message.
现在您拥有了您想要的内容,它适用于您的阵列中可能存在的任意数量的消息:
foreach ($messageData as $message) {
echo "Sender {$message['conversation_sender']} </br>";
// etc.
}
答案 1 :(得分:1)
对一个阵列使用foreach
,并在其他阵列中使用相同的键。
foreach ($messageData['conversation_sender'][0] as $index => $sender) {
$class = $messageData['conversation_class'][0][$index];
$time = $messageData['conversation_time'][0][$index];
$message = $messageData['conversation_time][0][$index];
// do stuff with those variables
}
然而,更为惯用的方式就像在gview的答案中一样。
答案 2 :(得分:1)
您可以使用foreach循环。
@foreach($messageData['conversation_sender'][0] as $key=>$sender)
{{--*/ $class=$messageData['conversation_class'][0][$key];
$message = $messageData['conversation_message'][0][$key];
$time = $messageData['conversation_time'][0][$key]; /*--}}
//Your code here
@endforeach
或直接使用php
foreach($messageData['conversation_sender'][0] as $key=>$sender){
$class=$messageData['conversation_class'][0][$key];
$message = $messageData['conversation_message'][0][$key];
$time = $messageData['conversation_time'][0][$key];
//your code here
}