Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, I will give you an example of a How to Combine array in laravel in the blade file, So you can easily apply it with your laravel 5, laravel 6, laravel 7, laravel 8, and laravel 9 application.
First, what we’re doing here, This is the example :
We will combine two arrays using the PHP array_combine() Function.
The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.
Note:– Both arrays must have an equal number of elements.
Syntax:
array_combine(keys, values)
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
Combine array in foreach loop in Laravel Blade file
resources\views\test.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>combine two array in laravel foreach loop</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2 style="color: #BE206B;">Combine Array's in foreach loop in the Blade File | Laravel</h2>
@php
$user_names = array('John','David','Sachin','Gaurav');
$user_emails = array('john@gmail.com','david@gmail.com','sachin@gmail.com','8bityard@gmail.com');
@endphp
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
@foreach (array_combine($user_names, $user_emails) as $user_name => $user_email)
<tbody>
<tr>
<td>{{ $user_name ?? '' }}</td>
<td>{{ $user_email ?? '' }}</td>
</tr>
</tbody>
@endforeach
</table>
</div>
</body>
</html>
Related article:- Get data from two models in Laravel.
In this article, we learned “merge two arrays in laravel or laravel combine two arrays example”, I hope this article will help you with your Laravel application Project.
Read also:- Access application in maintenance mode using a secret token in Laravel.