Jump to content

Remove row from array and move next value into place etc


mschrank99

Recommended Posts

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

 

 

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.

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.

<?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>';

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.