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. Link to comment https://forums.phpfreaks.com/topic/208472-overwriting-an-string-pointer-with-an-array-item/ 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 Link to comment https://forums.phpfreaks.com/topic/208472-overwriting-an-string-pointer-with-an-array-item/#findComment-1089304 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* Link to comment https://forums.phpfreaks.com/topic/208472-overwriting-an-string-pointer-with-an-array-item/#findComment-1089317 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. Link to comment https://forums.phpfreaks.com/topic/208472-overwriting-an-string-pointer-with-an-array-item/#findComment-1089335 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.