law Posted July 21, 2010 Share Posted July 21, 2010 I have some for loops that are rolling through various arrays. When a certain array value satisfies an if statement, I would like to overwrite another array with information from that array. Rough Example: $num1 = 30; //just example data $b[3] = 20; $b[6] = 15; $a = 120358; //number string given to me for($i=0; $i < $num1; $i++){ if(isset($b[$i])){ //when it finds that $b has data $a[$i] = $b[$i]; // I need to replace $a[$i] with b's data. I have a large number of processes that go below this point that rely on $a. What is the best way to do this? The current example sets $a[$i] = 2 NOT 20 because A is a string not really an array. } } Ideas? or a fundamental concept issue? I am open to suggestions. Quote Link to comment Share on other sites More sharing options...
freelance84 Posted July 21, 2010 Share Posted July 21, 2010 If $a is not an array then if you put $a=$b[$i] ? not sure what you want Quote Link to comment Share on other sites More sharing options...
law Posted July 21, 2010 Author Share Posted July 21, 2010 In this example $a = 120358 a[3] = 3 b[3] = 20 a[3] = b[3] a[3] = 2 <- not 20 I am trying to replace a specific section of a string with essentially another string. Does that make sense? I would assume there is some functions I am missing. *Going to re-check the manual* Quote Link to comment Share on other sites More sharing options...
law Posted July 21, 2010 Author Share Posted July 21, 2010 On the odd chance this might help someone, I used str_split($str, 3) to convert $a to a string. Then used array_splice($a, $i, 1, $b[$i]); to swap out the two values. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.