tags:

views:

101

answers:

2

Ok, I need keys to be preserved within this array and I just want to shift the 1st element from this array. Actually I know that the first key of this array will always be 1 when I do this:

// Sort it by 1st group and 1st layout.
ksort($disabled_sections);
    foreach($disabled_sections as &$grouplayout)
        ksort($grouplayout);

Basically I'd rather not have to ksort it in order to grab this array where the key = 1. And, honestly, I'm not a big fan of array_shift, it just takes to long IMO. Is there another way. Perhaps a way to extract the entire array where $disabled_sections[1] is found without having to do a foreach and sorting it, and array_shift. I just wanna add $disabled[1] to a different array and remove it from this array altogether. While keeping both arrays keys structured the way they are. Technically, it would even be fine to do this:

$array = array();
$array = $disabled_sections[1];

But it needs to remove it from $disabled_sections. Can I use something like this approach...

$array = array();
$array = $disabled_sections[1];
$disabled_sections -= $disabled_sections[1];

Is something like the above even possible??

Thanks.

+2  A: 

While there's no -= operator in that fashion, you can use unset to remove that element from an array:

unset(disabled_sections[1]);

But that's just implementing your own version of shift. I do wonder under what situation you're finding array_shift() to be 'slow' and how you're testing said slowness.

Numeric arrays are sorted numerical by default - no ksort is required. Maybe you should try something like

while($array = array_shift($group_of_arrays)) {

  // ... do stuff
}
Erik
Thanks, I don't know why I didn't think of that...lol. I was reading comments on array_shift located here: http://php.net/manual/en/function.array-shift.php, and a lot of people are saying it's too slow there. I don't want it to be slow on my end either.Thanks again :)
SoLoGHoST
Ok, I don't think `array_unshift` would help me here. I'm talking about `array_shift`, not `array_unshift`.
SoLoGHoST
`array_unshift` was a typo :) array_shift is only 'slow' against VERY large arrays, which is why I asked under what situation you were using it .. ie. an array with 500 items, or an array with 500,000 items.
Erik
furthermore, the numbers are coming from the database, where the first key is an id value from a column in the database. Cheers, you reminded me of the `unset()`. I feel totally dumb now... hehe ;)
SoLoGHoST
Yeah, that's what I thought. No harm done... Cheers :)
SoLoGHoST
The array can have a million items really. Well, maybe that's an exaggeration. But there's really no limit to the array as it grabs all rows from a table... so it's needed to be fast, not slow. Thanks, unset should do the trick than :)
SoLoGHoST
A: 

Despite there being an accepted answer to this; in case someone else stumbles across this, a way to unset the first element of an array (regardless of its key, or the order of its keys) without using array_shift is:

reset($array); // sets internal array pointer to start
unset($array[key($array)]); // key() returns key of current array element

Though I'm fairly convinced that's what array_shift does internally (so I imagine there would be no performance gain to this), excepting an additional return of the value retrieved:

$element = reset($array); // also returns element
unset($array[key($array)]);
return $element;

Just for completion's sake.

pinkgothic
That's not *quite* what what array_shift does, shift does all those things PLUS renumbers the entire array in the case of a numeric array - which is why it can be slow on large arrays.
Erik
I tend to work with associate arrays as exclusively as possible, so I had no idea. :3 Good to know!
pinkgothic