debz89uk Posted December 28, 2010 Share Posted December 28, 2010 Okay so here is a very simplified version of my code in order to try and illustrate what I am trying to do: $C1 = array (174,30); $C2 = array (165,80); $S = array($C1, $C2); foreach ($S as $cluster) { $number = '5000'; $another_number = '4565'; $cluster[0] = $number; } I feel like this should be really simple but its not working. So basically I'm trying to change the first value of the array select (e.g. C1 or C2) to whatever is in the variable $number. Any help would be greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 To operate on the actual elements of the array $S, you need to use a reference (otherwise $cluster is a copy of the data) - foreach ($S as &$cluster) You could also use the foreach ($S as $key => $cluster) syntax and then use the $key to modify $S directly - $S[$key][0] = $number; Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/#findComment-1152322 Share on other sites More sharing options...
DavidAM Posted December 28, 2010 Share Posted December 28, 2010 Watch out when you do that. After the loop finishes, $cluster still refers to the last element of the array. If you decide to use that variable again you will be affecting the array. It is a good idea to unset() the loop variable ($cluster) immediately after the loop to prevent unwanted side affects. (This one bit me the other day and it took me a while to track it down). $C1 = array (174,30); $C2 = array (165,80); $S = array($C1, $C2); foreach ($S as &$cluster) { $number = '5000'; $another_number = '4565'; $cluster[0] = $number; } // unset($cluster); # Really need to unset the reference here // more code that does other stuff foreach ($somethingElse as $cluster) { // You have now put the first element of $somethingElse into the $S array above Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/#findComment-1152327 Share on other sites More sharing options...
debz89uk Posted December 29, 2010 Author Share Posted December 29, 2010 Watch out when you do that. After the loop finishes, $cluster still refers to the last element of the array. If you decide to use that variable again you will be affecting the array. It is a good idea to unset() the loop variable ($cluster) immediately after the loop to prevent unwanted side affects. (This one bit me the other day and it took me a while to track it down). $C1 = array (174,30); $C2 = array (165,80); $S = array($C1, $C2); foreach ($S as &$cluster) { $number = '5000'; $another_number = '4565'; $cluster[0] = $number; } // unset($cluster); # Really need to unset the reference here // more code that does other stuff foreach ($somethingElse as $cluster) { // You have now put the first element of $somethingElse into the $S array above I've tried both of these and nothing seems to work. It is still returning the same entries in the array that were there originally? Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/#findComment-1152571 Share on other sites More sharing options...
DavidAM Posted December 29, 2010 Share Posted December 29, 2010 What version of PHP are you using? The reference in a foreach was added in version 5. If you are using version 4, you will have to use PFMaBiSmAd's second suggestion to affect the array: foreach ($S as $key => $cluster) { $S[$key][0] = 'new value'; } Do you have error reporting turned on? Are you getting any errors, warnings or notices? error_reporting(E_ALL); ini_set('display_errors', 1); Also, you do realize you are changing $S not $C1, right? The assignment of $C1 and $C2 to $S makes a copy of those arrays. If you are trying to change $C1 and/or $C2, you need to put references in $S: $C1 = array (174,30); $C2 = array (165,80); $S = array(&$C1, &$C2); Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/#findComment-1152581 Share on other sites More sharing options...
debz89uk Posted December 29, 2010 Author Share Posted December 29, 2010 What version of PHP are you using? The reference in a foreach was added in version 5. If you are using version 4, you will have to use PFMaBiSmAd's second suggestion to affect the array: foreach ($S as $key => $cluster) { $S[$key][0] = 'new value'; } Do you have error reporting turned on? Are you getting any errors, warnings or notices? error_reporting(E_ALL); ini_set('display_errors', 1); Also, you do realize you are changing $S not $C1, right? The assignment of $C1 and $C2 to $S makes a copy of those arrays. If you are trying to change $C1 and/or $C2, you need to put references in $S: $C1 = array (174,30); $C2 = array (165,80); $S = array(&$C1, &$C2); Sorry. Yes I am trying to change $C1. How do I do this? Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/#findComment-1152583 Share on other sites More sharing options...
PFMaBiSmAd Posted December 29, 2010 Share Posted December 29, 2010 DavidAM told and showed you in his post how you would change $C1. However, you should NOT be making a series of numbered variables to hold individual data arrays. You should be directly storing that data as an array of arrays (the $S array, using index values that identify which data set it belongs to.) If you post what you are really trying to do (where the data is coming from and where you are trying to put the results) you will get to the final solution quicker. Quote Link to comment https://forums.phpfreaks.com/topic/222846-arrays-in-arrays/#findComment-1152594 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.