1
        array:3 [▼
                "name" => array:3 [▼
                              0 => "user2"
                              1 => "user3"
                              2 => "user4"
                                 ]
                "email" => array:3 [▼
                              0 => "[email protected]"
                              1 => "[email protected]"
                              2 => "[email protected]"
                                  ]
                "phone_number" => array:3 [▼
                              0 => "90352065"
                              1 => "69856352"
                              2 => "903520658"
                                  ]
              ]

I'm getting above response from multiple text boxes now I need to loop in view, so How to loop above arrays in view??

how can I display above array values like in below format

       User2 details
             name  : user2
             email : [email protected]
             phone_number : 90352065
       User3 details
             name  : user3
             email : [email protected]
             phone_number : 69856352 
       User4 details
             name  : user4
             email : [email protected]
             phone_number : 903520658
4
  • 1
    Simply, use nested foreach Commented Jun 11, 2017 at 16:02
  • 1
    How to use nested foreach??I'm new to laravel. Commented Jun 11, 2017 at 16:04
  • Please, describe what kind of output you need Commented Jun 11, 2017 at 16:05
  • 1
    I need to display all names,emails and phone numbers from above response in view, Commented Jun 11, 2017 at 16:07

3 Answers 3

4

I presume below is your array structure

$original_array = [
  "name" => [
      "user2","user3","user4"
  ],
  "email" => [
      "[email protected]","[email protected]","[email protected]"
  ],
  "phone_number" => [
    "90352065","69856352","903520658"
  ]
];

now to get desired output you can use below code

<dl>
@foreach($original_array['name'] as $key => $name)
  <dt>{{ $name }} Details</dt>
  <dd>
    <ul>
      <li>Name: {{ $name }}</li>
      <li>Email: {{ $original_array['email'][$key] }}</li>
      <li>Phone Number: {{ $original_array['phone_number'][$key] }}</li>
    </ul>
  </dd>
@endforeach
</dl>
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think this will be human-readable no matter how you will name the variables
The above answer is correct only, But I want to display in view like name :user2,email:[email protected],phone:903520658, name :user3,email:[email protected],phone: 69856352 ..
Please check my question again I have updated.Thank you
4

kind of

@foreach($nameArray as $index => $nameArrayElement)     
    <tr>
        <td>{{ $mainArray['name'][$index] }}</td>       
        <td>{{ $mainArray['email'][$index] }}</td>      
        <td>{{ $mainArray['phone_number'][$index] }}</td>   
    </tr> 
@endforeach

Take in mind that the number of names are equal to the number of rows

Comments

1

In laravel I would use this approach to recurse a nested array in blade:

<ul>
@forelse($elements as $key => $item)
    <li>
    @if(is_array($item))
        <strong>{{ $key }}: </strong>
        @include('admin.payments.confirmation-data', ['elements' => $item, 'parentKey' => $key])
    @else
        <strong>{{ $key }}: </strong><span>{{ $item }}</span>
    @endif
    </li>
@empty
    <li>
        <strong>yay {{ $parentKey }}</strong>:<span>[]</span>
    </li>
@endforelse
</ul>

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.