Re: Deleting elements from the middle of an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





how about creating two arrays, one empty one.
pop the elements
you want out of the first array and push them to the second.  skip
the push on the elements you don't want in the second array?

Just a thought.

--curtis

Paul M Foster wrote:
> On Mon, Mar 14, 2011 at 09:34:33PM +0100, Peter Lind wrote:
> 
>> On 14 March 2011 21:31, Paul M Foster
<paulf@xxxxxxxxxxxxxxxxx> wrote:
>> > Here's what I
need to do: I have an indexed array, from which I need
>> to
>> > delete elements in the middle. Once completed, the indexes
should be
>> > numerically in sequence, as they were when I
first encountered the
>> > array. That is:
>>
>
>> > Before:
>> > $arr = array(0 => 5,
1 => 6, 2 => 7, 3 => 8, 4 => 9, 5 => 10);
>>
>
>> > After:
>> > $arr = array(0 => 5,
1 => 6, 2 => 9, 3 => 10);
>> >
>>
>
>> > I've tried:
>> >
>> >
1) Using for() with unset(). Elements are deleted, but the indexes are
>> > no longer sequential.
>> >
>>
> 2) Using for() with array_splice(). Understandably, deletes
certain
>> > elements but not others.
>> >
>> > 3) Using foreach() with referenced array elements. Neither
unset() nor
>> > array_splice() appear to have any effect on
the array at all.
>> >
>> > 4) while() loop
using current() and the like. But these array
>> functions
>> > return values, not references, so the array isn't actually
modified.
>> >
>> > 5) array_walk() with
unset() array_splice(). No effect on the array.
>> >
>> > Anyone know how to do this, or know of a reference on how
to?
>> >
>>
>> Remove the elements,
then use sort().
> 
> I've given a simplified example. The
actual target array is
> multi-dimensional. Sort() won't work in a
case like that, as far as I
> know. Moreover, I don't want the
array sorted based on the element
> values.
> 
>
Paul
> 
> --
> Paul M. Foster
>
http://noferblatz.com
> http://quillandmouse.com
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux