Jump to content

Key change of a multidimensional array reverts back


Slips

Recommended Posts

Hello,

 

I'm a new  building my career as a web developer/designer and i'm attempting to create an array that holds different attributes of all the web projects.

 

Site1 => array('www.siteurl.com','hostAccountName','Offline domain name',) ,

Site2 => array('www.site2url.com','hostAccoun2tName','Offline domain2 name') ,

Site3 => array('www.site2url.com','hostAccount3Name','Offline domain3 name') ,

 

I have many sites in the multidimensional array BUT each has the same format of attributes. I store this entire array in one file that gets included, and based on some logic later , the script will decide which site i'm currently working on and thus that site's attributes must be used, since i will just use one variable where these attributes are needed and leave it upto some logic to decide which site's attributes to use.

 

 

Objective of the code : To change the array keys of each site's attributes from 0,1,2 etc to more readable strings for later use to directly access values. I'm creating an array with the values first and then running some code to change the keys later because i might decide to change the key names later and i can't manually edit it for each of the array values because the array might grow much larger.

 

Site1 => array ([0] => 'www.siteurl.com',

                          [1] => 'username@hostserver',

                          [2] => 'siteOfflineDomain');

 

must change into

 

Site1 => array (['siteUrl] => 'www.siteurl.com',

                          ['hostingUserName'] => 'username@hostserver',

                          ['OfflineVirtualHost'] => 'siteOfflineDomain');

 

And this must happen in a foreach loop and go through all the sites to change all the default 0,1,2 indices, to the index names i provide.

 

 

Code :

$siteList = array (
		'site1'     => array
					(
						'www.site1.com',
						'site1@host',
						'site1_offline'
				      ),
				  
		'site2' => array 
					(
						'www.site2.com',
						'site2@host',
						'site2_offline'

					),

		'site3'  => array
					(
						'www.site3.com',
						'site3@host',
						'uttarkar_offline'
					)					  
		);



foreach ($siteList as $sitePrefix => $siteAttributes) {
foreach ($siteAttributes as $oldIndex => $attribute) {
	switch ($oldIndex) {
		case 0 :
			$siteAttributes['siteUrl'] = $siteAttributes[$oldIndex];
			unset($siteAttributes[$oldIndex]);
			break;
		case 1 :
			$siteAttributes['hostserverUserName'] = $siteAttributes[$oldIndex];
			unset($siteAttributes[$oldIndex]);
			break;
		case 2 :
			$siteAttributes['offlineDomainName'] = $siteAttributes[$oldIndex];
			unset($siteAttributes[$oldIndex]);								
			break;
	}
}

echo 'The newly edited attributes array for ',$sitePrefix,'is '.print_r($siteAttributes),'<br><br>';
}

 

Problem : This script works great, and the indices for the attributes get changed as expected, as you will see in the echo statement. But if i do a echo $siteList['site1']['siteUrl'] after the code, it says 'unknown index siteUrl'. Then i check the entire array with print_r($siteList) , and all the index keys are back to being 0,1,2!. My guess is that theres something i'm missing here to make them stick, but i spent 3 hours trying to figure it out, and i was getting a bit impatient, so can i get some help on this one please :), thanks.

Link to comment
Share on other sites

I like to point out the function of "foreach" from php.net/foreach

 

"Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it. "

 

IE: you'll want to pass by reference if you wish to change the original array.

 

Edit: It might help to notice that your echo statement is WITHIN a foreach loop (so it references the copy of the array). You should be able to fix this by changing the one line:

 

foreach ($siteList as $sitePrefix => &$siteAttributes) {

 

This should pass the reference of siteAttributes without making a copy.

Link to comment
Share on other sites

Hmmm i thought about that as one a the reasons right from the start, but when i put it to the test , the array variable outside the foreach loop reflected changes in it.

 

$array = array('Apple','Watermelon');
foreach ($array as $names) {
$array['red'] = $array[0];
unset($array[0]);
$array['green'] = $array[1];
unset($array[1]);
break;
}

print_r($array);

 

That code displays this as Array ( ['red'] => Apple ['green'] => Watermelon ).

If the foreach makes a copy of the array for its use shouldn't i just be able to see the original array?

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.