How to get and change value of element array in Laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Array Manipulation in PHP: How to Get and Change Values Based on Another Array in Laravel

As developers working with data-driven applications, manipulating arrays efficiently is a fundamental skill. Whether you are handling form submissions, processing database results, or structuring complex configurations within a Laravel application, understanding how to synchronize values between multiple arrays is crucial.

The scenario you've presented—synchronizing the values of $array_2 based on specific conditions found in $array_1—is a very common data transformation task. While the logic can seem straightforward, the syntax and approach matter significantly for performance and readability.

This post will walk you through the correct, idiomatic ways to achieve this array manipulation in PHP, providing solutions that are robust and align with modern programming practices.

The Challenge: Synchronizing Two Arrays

Let's look at the arrays we are working with:

$array_1 = [1, 2, 3, 1, 2]; // The source array defining the conditions
$array_2 = [0, 0, 0, 0, 0]; // The target array we want to modify

The goal is to iterate through $array_1. If an element in $array_1 is either 1 or 2, we want to update the corresponding position in $array_2 to a specific value (in this case, 1).

Desired Output for $array_2: [1, 1, 0, 1, 1]

Method 1: The Classic Approach – Iterating with Index

The most direct way to modify an array at a specific index is by using a standard foreach loop combined with the array_values() or explicit indexing. Since we need to write back to $array_2, we must iterate over the indices of $array_1.

For this method, we use the array_keys() or simply iterate directly if we know both arrays are perfectly aligned by index (which is assumed in this case).

$array_1 = [1, 2, 3, 1, 2];
$array_2 = [0, 0, 0, 0, 0];

foreach ($array_1 as $index => $item) {
    // Check if the current item is 1 OR 2
    if ($item === 1 || $item === 2) {
        // If the condition is met, update the corresponding index in $array_2
        $array_2[$index] = 1;
    }
}

echo "Resulting array_2: " . print_r($array_2, true);
// Output: Resulting array_2: Array ( [0] => 1 [1] => 1 [2] => 0 [3] => 1 [4] => 1 )

Developer Insight: This method is highly explicit and performs well. By using the foreach ($array_1 as $index => $item) syntax, we gain access to both the value ($item) and its exact position ($index), allowing us to modify $array_2 directly at that corresponding location.

Method 2: The Functional Approach – Using array_map with array_filter (More Laravel Idiomatic)

While the loop above works perfectly, modern PHP and frameworks like Laravel encourage functional programming patterns when transforming data. For complex transformations, using higher-order functions like array_map or processing the arrays in parallel can lead to cleaner, more declarative code.

However, for this specific task (setting values based on a condition), we can first create a mask array and then use that mask to update the target array.

A slightly more advanced pattern involves creating a boolean mask:

$array_1 = [1, 2, 3, 1, 2];
$array_2 = [0, 0, 0, 0, 0];

// Step 1: Create a mask array based on the condition (is it 1 or 2?)
$mask = array_map(function ($item) {
    return ($item === 1 || $item === 2) ? 1 : 0;
}, $array_1);

// Step 2: Use array_map to update $array_2. We multiply the mask by the target value (1).
$array_2 = array_map(function ($value, $maskValue) {
    return $value * $maskValue;
}, $array_2, $mask);

echo "Resulting array_2: " . print_r($array_2, true);
// Output: Resulting array_2: Array ( [0] => 1 [1] => 1 [2] => 0 [3] => 1 [4] => 1 )

Laravel Context: When dealing with Eloquent collections or large datasets in Laravel, functional methods like map, filter, and where are heavily utilized. Understanding how to transform data using these concepts is key to writing scalable backend logic on https://laravelcompany.com. While the second method is more complex for this simple case, it demonstrates a powerful way to handle array transformations that scales well into larger data pipelines.

Conclusion

For synchronizing two parallel arrays in PHP, Method 1 (the explicit indexed foreach loop) remains the most readable and straightforward solution. It gives you direct control over which index is being modified.

If your requirements involve more complex filtering or transformation logic where you are creating entirely new structures rather than modifying existing ones, exploring functional methods like array_map will provide a more elegant, expressive approach. Always choose the method that best communicates your intent to other developers.