DCM Posted October 13, 2010 Share Posted October 13, 2010 Hi I have a multi-dimension session array as per the below: $_SESSION['myarray'][][] If for example there are several elements stored in the array: $_SESSION['myarray'][0][0] = "aaa"; $_SESSION['myarray'][0][1] = "111"; $_SESSION['myarray'][1][0] = "bbb"; $_SESSION['myarray'][1][1] = "222"; $_SESSION['myarray'][2][0] = "ccc"; $_SESSION['myarray'][2][1] = "333"; $_SESSION['myarray'][2][0] = "ddd"; $_SESSION['myarray'][2][1] = "444"; How can i remove one of the above and shunt the other elements up? I have tried using the unset command but this seems to essentailly nullify the desired values where as what i would like to do is remove them completly from the array and then shorten the total number of records stored in the array. Thanks for taking the time to read this. Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/ Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 Can you post the desired array outline? Like what you want it to end up being. Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/#findComment-1121827 Share on other sites More sharing options...
DCM Posted October 13, 2010 Author Share Posted October 13, 2010 Sure the above is simply and example but if example the array at present is as above such that the values assigned are $_SESSION['myarray'][0][0] = "aaa"; $_SESSION['myarray'][0][1] = "111"; $_SESSION['myarray'][1][0] = "bbb"; $_SESSION['myarray'][1][1] = "222"; $_SESSION['myarray'][2][0] = "ccc"; $_SESSION['myarray'][2][1] = "333"; $_SESSION['myarray'][2][0] = "ddd"; $_SESSION['myarray'][2][1] = "444"; A quick for loop to output the results given that $numrecs currently equals 4 for ($x=0; $x<$numrecs; $x++) { echo $_SESSION['myarray'][$x][0] ."<br>"; echo $_SESSION['myarray'][$x][1] ."<br>"; } would give as output: aaa 111 bbb 222 ccc 333 ddd 444 If say record $_SESSION['myarray'][1] has been deleted in the back end data base and $numrecs reduced by one, then on refresh of the webpage i would the contents of the array to display as: aaa 111 ccc 333 ddd 444 However at present i can't seem to get this to work, to delete the data in the array I do unset ($_SESSION['myarray'][1][0]); unset ($_SESSION['myarray'][1][1]); However on re-running the for loop i then get the below as output: aaa 111 ccc 333 When it should be: aaa 111 ccc 333 ddd 444 Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/#findComment-1121836 Share on other sites More sharing options...
DCM Posted October 13, 2010 Author Share Posted October 13, 2010 typo in presious reply, the original array is defined as : $_SESSION['myarray'][0][0] = "aaa"; $_SESSION['myarray'][0][1] = "111"; $_SESSION['myarray'][1][0] = "bbb"; $_SESSION['myarray'][1][1] = "222"; $_SESSION['myarray'][2][0] = "ccc"; $_SESSION['myarray'][2][1] = "333"; $_SESSION['myarray'][3][0] = "ddd"; $_SESSION['myarray'][3][1] = "444"; Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/#findComment-1121840 Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 Honestly, I think all you need to use is the sort method: $array[0][0] = "aaa"; $array[0][1] = "111"; $array[1][0] = "bbb"; $array[1][1] = "222"; $array[2][0] = "ccc"; $array[2][1] = "333"; $array[3][0] = "ddd"; $array[3][1] = "444"; unset($array[1]); sort($array); $numrecs = count($array); for ($x=0; $x<$numrecs; $x++) { echo $array[$x][0] ."<br>"; echo $array[$x][1] ."<br>"; } Should do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/#findComment-1121843 Share on other sites More sharing options...
DCM Posted October 13, 2010 Author Share Posted October 13, 2010 Thanks I'll give it a try.. I was also looking at array_splice which may also work without me having to sort the array. Idealy i do not want to disturb the order in which data is displayed in the array as it (in my real app) will have been pr-ordered by the user selecting from a sort by radio button selection. making progress though so thanks. Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/#findComment-1121846 Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 Well, if that is the case, I would use foreach: unset($array[1]); foreach ($array as $parent) { foreach ($parent as $child) { echo $child . '<br>'; } } Should give you your desired output. Quote Link to comment https://forums.phpfreaks.com/topic/215794-removing-elements-from-a-multi-dimension-array/#findComment-1121849 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.