How to replace string values from all array values?

Asked

Viewed 979 times

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".

The code : https://paste.ofcode.org/UnbrtXVGFE75N6QzizXbuM

  • 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']} ";

  • I really needed these two arrays, I could create a multidimensional array from two different arrays?

  • Yes with array_combine, I answered the question by showing an example.

  • 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

  • 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??

  • I didn’t get it either, just the str_replace() doesn’t solve? ex: $z = str_replace($nome, $valor, $x);&#xA;echo $z;

  • 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.

  • I’m sorry I wasn’t clearer on the point, I hope the definition in Edit 2 will help

Show 3 more comments

2 answers

1


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

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

$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 on Ideone.

Some considerations:

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

  • You were using ++$i instead of $i++, is not wrong, but runs away from the pattern.

  • Good afternoon Francisco, tried this way before already, the problem is that it saves only the last value , in the case "This is my value1 and my 2", would have some way to change the two values in the same string from the arrays? Grateful for the response and considerations

  • @viniciusmartin Have you tested the code I left? Here works exactly the way you want.

  • Yes, this solution was the first I thought of, but I was just printing out the last option after going through the array, I made some changes to the question that might help to see the problem I’m having ; )

  • @viniciusmartin Da a thelaugh here.

  • Yes, but here he takes the last value only, @Francisco olha so https://paste.ofcode.org/UnbrtXVGFE75N6QzizXbuM

  • @viniciusmartin Psé, but you’re not doing what I said. You’re creating an array, in my example I’m creating a string.

  • I do not believe, thank you very much from my heart, I was missing a small detail ahaha, thank you for the answer :)

Show 2 more comments

1

I think you could use a multidimensional array to solve this case, but if you can’t, maybe a tbm array_combine would 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 wouldn’t even need the loop. Now if you’re going to have more values, from one edited in your question by putting more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.