Combining two arrays using nested foreach loops I have the following arrays:

Array  (db_values)
(
[0] =&gt; Array
    (
        [system_name] =&gt; object_car_brand
        [value] =&gt; Alfa Romeo
        [id] =&gt; 136
    )

[1] =&gt; Array
    (
        [system_name] =&gt; object_car_model
        [value] =&gt; Spider
        [id] =&gt; 137
    )

)
Array (db_attributes)
(
[0] =&gt; Array
    (
        [id] =&gt; 105
        [system_name] =&gt; object_car_brand
    )

[1] =&gt; Array
    (
        [id] =&gt; 106
        [system_name] =&gt; object_car_model
    )

)


I combine these two using the following code:

       foreach($db_attributes as $db_attribute){
            foreach($db_values as $db_value){
                if($db_value["system_name"] === $db_attribute["system_name"]){
                    $update[$db_attribute["id"]] = $db_value["value"];
                }

            }

        }


I do not think that this is the most resource friendly way of doing it, is there a better way?
I have made the assumption that there is a 1:1 relationships between the $attributes and $values array elements. With that I mean the array key in the $attributes array corresponds with an entry in $values array.

If that is the case it can be reduced to one foreach loop by using the key from the attributes array:

$combined = []; // Make sure the $combined array exists.

foreach($attributes as $key =&gt; $attribute) {

    // First check if the array key exists and that the 'system_name' is the same
    if(array_key_exists($key, $values) &amp;&amp; $attribute['system_name'] == $values[$key]['system_name']) {

        $combined[$attribute['id']] = $values[$key]['value'];

    }

}


This should produce the following array with the data you have provided:

Array(combined)
(
    [106] =&gt; 'Alfa Romeo' 
)


If my assumption is incorrect, then just ignore my answer.