Jump to content

Arrays in arrays


debz89uk

Recommended Posts

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.