mschrank99 Posted October 20, 2007 Share Posted October 20, 2007 Hi, Let's say I have an array like this: $array[0] = "ball"; $array[1] = "bat"; $array[2] = "glove"; $array[3] = "referee"; $array[4] = "strike"; $array[5] = "home run"; Now, let's say I want to get rid of [3] but move the next values up, automatically, into 3,4. For example, $array[0] = "ball"; $array[1] = "bat"; $array[2] = "glove"; $array[3] = "strike"; $array[4] = "home run"; Is there a way to simply do this? I could do it using for loops, but is there a simpler way, like a built in function, that will do this for me? Thanks, Matthew Quote Link to comment Share on other sites More sharing options...
mschrank99 Posted October 20, 2007 Author Share Posted October 20, 2007 If you think there is no such function and I should just use a for loop to iteratively set the keys back one number after the one I delete, please tell me. I need to work on this today. I just want to save processing power and time if I can. Quote Link to comment Share on other sites More sharing options...
enoyhs Posted October 20, 2007 Share Posted October 20, 2007 http://w3schools.com/php/func_array_splice.asp I think this is what you are looking for. Quote Link to comment Share on other sites More sharing options...
mschrank99 Posted October 20, 2007 Author Share Posted October 20, 2007 Hmm, thanks, getting close, but I don't think that'd work. If I had two arrays, and wanted to cut out part of the first one or add the second to it, it'd work. I might just have to go with a for loop... gosh, I'm surprised. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 20, 2007 Share Posted October 20, 2007 <?php $array = array ("ball","bat","glove","referee","strike","home run"); unset ($array[3]); // remove 'referee' $array = array_values ($array); echo '<pre>', print_r($array, true), '</pre>'; ?> Quote Link to comment 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.