0

I am trying to use more than one array in a @foreach. After looking at SO I saw this is not what @foreach is for so what should I use in the following scenario? This is fake data I'm using to mock up the site before handing it to the back end devs. I am using Laravel and am a beginner in PHP so I am at your mercy.

Array

$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';

Example of @foreach loop I have been using, for one array at a time

@foreach($messageData['conversation_sender'][0] as $value)
    <span class="form-tag">{{ $value }} <i class="fa fa-plus"></i></span> 
@endforeach

How I'd like to use it(I know I'm dreaming)

@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

Example of what the first iteration should print out

<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>
5
  • 1
    looks like you can just use the keys from one loop\array to pull in the data from the other arrays. Commented Jun 10, 2016 at 0:30
  • your example ..didn't Clint smith say "Hello!" not " Can't wait to catch up" Commented Jun 10, 2016 at 0:31
  • Why do you have @ before everything? In PHP, @ is for suppressing error messages. Commented Jun 10, 2016 at 0:36
  • @Dagon Good catch :) Commented Jun 10, 2016 at 0:49
  • @Barmar I'm using Laravel, I don't know why really, PHP is black magic to me. Commented Jun 10, 2016 at 0:49

3 Answers 3

2

Restructure your array to be what it should be: hierarchical.

$messageData[] = array(
   'conversation_sender' => 'Clint Smith',
   'conversation_class' => 'owner-comment',
   'conversation_message' => 'Hello!',
   'conversation_time' => '2 minutes ago'
);
// Repeat for each other message.

Now you have exactly what you want, and it works for any number of messages that might exist in your array:

foreach ($messageData as $message) {
    echo "Sender {$message['conversation_sender']} </br>";
    // etc.
}
Sign up to request clarification or add additional context in comments.

3 Comments

nothing particularly bad about the current format, easy enough to use as is.
@Dagon, entirely depends on the data. My answer is based on the fact that the question clearly states this is mock data.
One can surmise from this that this is a system that is based on messages, not individual sub arrays of senders, messages and times. Depending on the same numeric indexes to locate the other entries associated with the same message can be done easily enough as per Barmar's answer, but at times there are conclusions that can be jumped to that are useful in my opinion, and this is one of those times where my assumption is that the structure of the data is not yet set in stone, and that an array of messages like an array of records is what is really desired here.
1

You can use the foreach loop.

    @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

Or use straight 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

}

5 Comments

Hi, why is code starting on the second line commented out on the first foreach? Thanks
Edited. @Clinton That's a trick used to assign variables in a Blade template or you can replace {{--*/ with <?php and the /*--}} with ?>
Magic :) Thanks for explaining
@ClintonGreen should of accepted Barmer's, same thing posted half hour earlier
@Dagon I tried all the answers, Barmars is very similar but didn't work in my code. Probably because the variables were not wrapped in <?php
1

Use foreach for one array, and use the same keys in the other arrays.

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
}

However, the more idiomatic way to do it is as in gview's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.