How to replace values of a string from all values of an array?

1

Good afternoon guys, well the problem is the following ..
I have a string named x, and I have two arrays, name and value. in both arrays I identify if in the string X has some field equal to $ name, if it has substitution by $ value, Is there any way to run it when it overwrites and saves in a single string all changed fields? Sample code below: Note the string is an array, so it replaces the values one by one.

    $valor = array('1', '2');
    $nome = array('valor1', 'valor2');
    $x = "Esse é meu valor1 e meu valor2";
    for ($i=0; $i<$cc; ++$i) {

    $string[$i] = str_replace($nome[$i], $valor[$i], $x);
    echo $string[$i];
    echo "<br />";


}

Answer that I want="This is my 1 and my 2";   Answer that goes out the way the code is="This is my 1 and my value2"                                                 "This is my value1 and my 2"

Edit: The values I placed there were for simplifying the problem, $ name and $ value are dynamic, can have N values, $ cc is the number of elements that make up both arrays

Edit 2: Sorry for not being clearer, the question I'm trying to solve is this, I have string with name Ex: "Component + General cost", $ name is the name of each component, eg $ name ('Component', 'General costs'), I have inputs that are generated dynamically, I'm rescuing these inputs from the value typed and the id of the component that dps got the name in the database, so for each component (every $ name) I have a value, what I really want is for example it will get the name arrays and replace with the corresponding value of array $ value (that hi $ would represent the position of both in array)

Edit 3: I have the values 0 => 10,1 => 20 in the array $ value, and 0 => Costs, 1 => Expenses in the $ name array, and a string named "Costs - Expenses, "for i $ would not replace both, and would be the final value of the string" 10 - 20 ", is what I want to get, since he is currently generating" 10 - Expenses "and in sequence" Costs - .

The code: link

    
asked by anonymous 02.10.2017 / 21:33

2 answers

1

This happens because you are creating an array of different strings in $string .

You can fix this using only one variable, in that case I used $x itself:

$valor = array('1', '2');
$nome = array('valor1', 'valor2');
$x = "Esse é meu valor1 e meu valor2";
for ($i = 0; $i < $cc; $i++) {
    $x = str_replace($nome[$i], $valor[$i], $x);
}
echo $x; //Saída: "Esse é meu 1 e meu 2"
  

See working at Ideone .

Some considerations:

  • You were giving echo within for , so 2 strings appeared.

  • You were using ++$i instead of $i++ , not wrong, but far from the default.

02.10.2017 / 21:44
1

I think you could use a multidimensional array to solve this case, but if you can not, maybe an array_combine would also work:

 $valor = array('1', '2', '3', '4', '5');
 $nome = array('valor1', 'valor2', 'valr3', 'vl4', 'v5');
 $valores = array_combine($nome, $valor);

echo "Esse é meu {$valores['valor1']} e meu {$valores['valor2']}, mas tenho também o {$valores['valr3']}, junto com {$valores['vl4']} e {$valores['v5']}";

You would not even need the loop. Now if you are going to have more values, than one edited in your question putting more details.

    
02.10.2017 / 21:53