xProteuSx Posted November 10, 2011 Share Posted November 10, 2011 I've got a data array that looks like this: $arraylist = ('00101001','10001010','00010100','01100101'); And another that looks like this: $arrayposition = (0,3); The idea is to loop through $arraylist and when the loop gets to the indexes/positions listed in the $arrayposition array, to change all of the 1's to 0's, then put the values back into a string variable. I have something like this at the moment: $chores = 1; $arraylist_new = ''; foreach ($arrayposition as $value) { if ($chores != '') { $oldvalnum = 0; foreach($arraylist as $string) { if ($value == $oldvalnum) { $string = str_replace("1", "0", $string); if ($arraylist_new == '') {$arraylist_new = $string;} else {$arraylist_new .= ', ' . $string;} } else { if ($arraylist_new == '') {$arraylist_new = $string;} else {$arraylist_new .= ', ' . $string;} } $oldvalnum++; } } } echo $arraylist_new; The idea is for the output to be: 00000000, 10001010, 00000000, 01100101. I know the code is hinda hectic, but could someone please help?? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/ Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2011 Share Posted November 10, 2011 Yeah, you've got that way over complicated. If it's just as simple as you've described, it can be done with one foreach() loop using str_replace, strlen and implode. There are probably other ways to do it as well, but this is what I came up with of the top of my head. Note that this only works if the values in $arraylist are quoted strings. <?php $arraylist = array( '00101001', '10001010', '00010100', '01100101' ); $arrayposition = array( 0,3 ); foreach( $arrayposition as $v ) { $arraylist[$v] = str_repeat( '0', strlen($arraylist[$v]) ); } $arraylist_new = implode( ', ', $arraylist ); echo $arraylist_new; Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/#findComment-1286941 Share on other sites More sharing options...
xProteuSx Posted November 10, 2011 Author Share Posted November 10, 2011 Pikachu2000 (lol), What would you say if I told you that, instead of 0's and 1's ... $arraylist = ( [0] => 26|7|0|10|0|0 [1] => 3|1|3|5|0|0 [2] => 3|2|3|5|0|0 [3] => 3|3|3|5|0|0 ) And that I was trying to switch the 0 to a 1 at index 4 ?? So, for example, if $arrayposition = array( 0,3 ); The I would like to update $array list from $arraylist = ( [0] => 26|7|0|10|0|0 [1] => 3|1|3|5|0|0 [2] => 3|2|3|5|0|0 [3] => 3|3|3|5|0|0 ) to $arraylist = ( [0] => 26|7|0|10|1|0 [1] => 3|1|3|5|1|0 [2] => 3|2|3|5|0|0 [3] => 3|3|3|5|0|0 ) ???? I'm sorry, but I simplified the code that I posted (insert rant about over-complication here) so yeah, values in $arraylist are not in quoted strings. Forgive me?? .... thank you by the way. Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/#findComment-1286942 Share on other sites More sharing options...
xProteuSx Posted November 10, 2011 Author Share Posted November 10, 2011 Forgot to mention (but you probably figured this out): I am breaking down the values in the array into sub-arrays, divided by the '|' character ... Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/#findComment-1286943 Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2011 Share Posted November 10, 2011 And there I was thinking I had come up with a nice, elegant solution. Can you post the output of this, using your actual data, or at least part of it? echo '<pre>'; print_r($arraylist); echo '</pre>'; I see what you're trying to do, but what determines the array element that needs to be changed? Is it simply one higher than the highest value in $arrayposition? Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/#findComment-1286944 Share on other sites More sharing options...
xProteuSx Posted November 10, 2011 Author Share Posted November 10, 2011 Pikachu2000 (lol, again!) You did come up with an elegant solution! I figured it out, BASED ALMOST ENTIRELY ON YOUR ELEGANT SOLUTION! Here's what I've got: <?php $arraylist = array('26|7|0|10|0|0','3|1|3|5|0|0','3|2|3|5|0|0', '3|3|3|5|0|0') ; $arrayposition = array( 0,1 ); //foreach( $arrayposition as $v ) // { // $arraylist[$v] = str_repeat( '0', strlen($arraylist[$v]) ); // } foreach ($arrayposition as $v) { $onechore = explode("|", $arraylist[$v]); $onechore[4] = 1; $chore = implode("|", $onechore); $arraylist[$v] = $chore; } $arraylist_new = implode( ', ', $arraylist ); echo $arraylist_new; ?> Output is: 26|7|0|10|1|0, 3|1|3|5|1|0, 3|2|3|5|0|0, 3|3|3|5|0|0 Magic! Thank you dear sir. Thank you, thank you, thank you. I've spent about 5 hours on this and I've come up with variations that go up to 100+ lines of code. Now I've got a beauty in about 15 lines. PS - you said you hate everything, yet you appear to like candy ... Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/#findComment-1286945 Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2011 Share Posted November 10, 2011 OK, you caught me. I hate almost everything. Quote Link to comment https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/#findComment-1287067 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.