1
Good afternoon guys, well the problem is this..
I have a string with name x, and I have two arrays, name and value.
in for perco both arrays where I identify if in string X has any field equal to $name, if you have replaced it with $value,
Is there any way when run it replaces and saves in a single string all fields changed? Example code below:
Note the string is as an array so it replaces one-at-one values.
$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 I want = "This is my 1 and my 2"; Answer that goes the way the code is = "This is my 1 and my value2" "This is my value1 and my 2"
Edit: Personal the values I put there were for simplification of the problem, the $name and $value are dynamic, can have N values, $cc is the number of elements that compose both arrays
Edit 2: Sorry I wasn’t clearer, the question I’m trying to solve is this, I have string with name Ex: "Component + General Cost", the $name is the name of each component, ex: $name('Component','General Cost'), I have inputs that are generated dynamically, i to rescue from these inputs the entered value and component id that dps get the name in the database,so for each component(each $name) I have a value, what I really want is for example, it will take the name arrays and replace with the corresponding value of the $value array( that i$ would represent the position of both in the array)
Edit 3: I have the values 0=>10,1=>20 in the $value array, and 0 =>Costs, 1=>Expenses in the $name array, and a string named "Costs - Expenses", for i$ no for would replace both, and would be the final value of the string "10 - 20", is what I want to get, since it is currently generating "10 - Expenses " and "Costs - 20".
obligatorily you need these two arrays?? Mount a single multidimencional array wouldn’t solve?? of the type
$valor = array('valor1' => '1', 'valor2' = '2');
, and then instead of you using it like this:$x = "Esse é meu {$valor['valor1']} e meu {$valor['valor2']} ";
– Fernando VR
I really needed these two arrays, I could create a multidimensional array from two different arrays?
– Vinicius Gularte
Yes with array_combine, I answered the question by showing an example.
– Fernando VR
I gave an Edit in question, both arrays may have X elements, which are defined by $cc, had put that way just to simplify the same, att
– Vinicius Gularte
I understand but what is confused is your phrase
Esse é meu valor1 e meu valor2
, pq in the sentence you only look for the values 1 and 2, and if the array has value 3, value 4 much more as you want q to be displayed on the screen?? , From what I understand the $name array would be the name of the keys q will search for the values that are at the same position of the variable $correct value??– Fernando VR
I didn’t get it either, just the
str_replace()
doesn’t solve? ex:$z = str_replace($nome, $valor, $x);
echo $z;
– rray
It’s q doesn’t have much logic, because the two arrays can be dynamic, but the phrase only asks for 2 values. I edited my answer by adding more sample values.
– Fernando VR
I’m sorry I wasn’t clearer on the point, I hope the definition in Edit 2 will help
– Vinicius Gularte