rhyspaterson Posted July 1, 2008 Share Posted July 1, 2008 Hey guys, I have a multidimensional array as follows: Array ( [line1] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) [foo] => Array ( [0] => h [1] => i [2] => j [3] => k [4] => l [5] => m ) [bar] => Array ( [0] => n [1] => o [2] => p [3] => q [4] => r [5] => s ) ); I would like to rename the foo and bar keys to line2 and line3 respectively - however i have absolutely no idea. Any suggestions? Cheers Link to comment https://forums.phpfreaks.com/topic/112713-solved-rename-array-key/ Share on other sites More sharing options...
interpim Posted July 1, 2008 Share Posted July 1, 2008 I don't understand the problem? Array ( [line1] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) [line2] => Array ( [0] => h [1] => i [2] => j [3] => k [4] => l [5] => m ) [line3] => Array ( [0] => n [1] => o [2] => p [3] => q [4] => r [5] => s ) ); Link to comment https://forums.phpfreaks.com/topic/112713-solved-rename-array-key/#findComment-578825 Share on other sites More sharing options...
rhyspaterson Posted July 1, 2008 Author Share Posted July 1, 2008 The array has already been created. I want to modify the name of the two keys. See below: $data = array( 'line1' => array('a', 'b', 'c', 'd', 'e', 'f'), ); for ($x = 0; $x < 2; $x++){ $data_temp = array('a', 'b', 'c', 'd', 'e', 'f'); data[] = $data_temp; } // this will output Array ( [line1] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) [0] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) [1] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) ); I have an array called $data, i then append a second array called $data_temp onto it. However it gives the arrays the names of 0 and 1, but i would like to change them. I'm fairly sure you can't give them names in the FOR loop.. Link to comment https://forums.phpfreaks.com/topic/112713-solved-rename-array-key/#findComment-578839 Share on other sites More sharing options...
rhyspaterson Posted July 1, 2008 Author Share Posted July 1, 2008 Edit: figured it out: $data["line". $x] Link to comment https://forums.phpfreaks.com/topic/112713-solved-rename-array-key/#findComment-578842 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.