Montiero Posted February 10, 2009 Share Posted February 10, 2009 Anybody got any idea how to do this? Essentially, I'm constructing a delete function for a link directory and was wondering how you'd delete a variable from an array. The way I've had to do it is by running a foreach() loop, followed by an "if" statement to rewrite each variable of an array. The problem is blank lines keep slipping through and messing up the script's display. I know the foreach/if routine will be required to identify the variable to be deleted, but does anyone know how I can directly delete a variable instead of just omitting it? Another problem is the loop runs an unnecessary amount of times, casing the script to take longer to load. Quote Link to comment https://forums.phpfreaks.com/topic/144646-solved-deleting-a-variable-from-an-array/ Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 unset if you know the index of the variable. <?php foreach ($array as $index => $val) { unset($array[$index]); } ?> Should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/144646-solved-deleting-a-variable-from-an-array/#findComment-759008 Share on other sites More sharing options...
Montiero Posted February 11, 2009 Author Share Posted February 11, 2009 For some reason my test with a pre-set array will work 100% fine on my localhost, but not on my server. I've got PHP 5.2 installed on my server and resources say unset has worked since around PHP 4, so I don't get it. Anyone know why this might be? Quote Link to comment https://forums.phpfreaks.com/topic/144646-solved-deleting-a-variable-from-an-array/#findComment-759389 Share on other sites More sharing options...
Philip Posted February 11, 2009 Share Posted February 11, 2009 What does your code look like? Quote Link to comment https://forums.phpfreaks.com/topic/144646-solved-deleting-a-variable-from-an-array/#findComment-759397 Share on other sites More sharing options...
Montiero Posted February 11, 2009 Author Share Posted February 11, 2009 I've just cracked it. I thought it was unset, but it was where I used str_replace to remove page breaks. The stored variables I was using to compare to the variable I was looking to delete contained a line break because it was at the end of a string in a text file. I did a quick google and realised there were a couple of ways to declare a line break. I created an array with all of those in and str_replace'd the array in each string and it worked. It also solved a two week problem of trying to parse my documents for any blank lines that may have slipped in. I literally made that discovery two minutes after posting, so I do apologise if I've wasted time. Quote Link to comment https://forums.phpfreaks.com/topic/144646-solved-deleting-a-variable-from-an-array/#findComment-759405 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.