Replace placeholders in array with values from other array

Alex Kneller

I have 2 arrays one with placeholder that are keys in another array

arr1 = array(
    "id"       => "{{verticalId}}",
    "itemPath" => "{{verticalId}}/{{pathId}}/");

arr2 = array(
        "verticalId" => "value1",
        "pathId"     => "value2");

So how can I run on arr1 and replace placeholders with value from arr2 ?

Barmar
foreach ($arr1 as $key => &$value) {
    $value = preg_replace_callback('/\{\{(.*?)\}\}/', function($match) use ($arr2) {
        return $arr2[$match[1]];
    }, $value);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replace values of a numpy array by values from another numpy array

Replace values in array using mask and other array

Replace elements in array by values from other array

How to replace values in my array of objects using key from other object in another array

How replace values from one numpy array by other array with indices

Replace Text with Other Values in Numpy Array

PHP replace string with values from array

Receive values from array between other values

Replace object value with values from an external array

Create new array with values from other array

Replace substrings with values from array

PHP replace string with values from mutiple array

Replace string to values from array

replace keys in array from other array php

Handlebars access nested array values by index in placeholders

Use jq to replace array values from dictionary

Replace all occurrences of substring with values from array

How to replace data from array to other array?

How to replace item in array with other values in spark?

Replace text with values from array in Bash

Replace all same values in array with other same values

Replace a string from array values in Javascript

jq to replace array key values from file

PHP Replace array values from object

Replace values from a string if they are present in an array of variables

how to replace arrays by pointers from other array

jq: how to replace keys with values ​from other keys whose ​are in strings of some array

Replace values in array of object with values from another array in JavaScript

PHP: Replace Associative Array Values with Values from indexed Array