FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » arrays with holes
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
arrays with holes [message #176741] Fri, 20 January 2012 20:42 Go to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
still! a php newbie. This is kind of a touch base question. I think
I have it...

I have an indexed array in which I progressively remove elements with

unset($my_array[$index]);

This leave "holes", which I'm ok with.

To now find the 5th element in this shot-up array I use

$my_array_piece = array_splice($my_array, 5, 1);

Is that how you "do" it?

Unfortunately, the "indexes" have meaning. I don't want to repack.
Re: arrays with holes [message #176742 is a reply to message #176741] Fri, 20 January 2012 20:44 Go to previous messageGo to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
On Jan 20, 2:42 pm, cate <catebekens...@yahoo.com> wrote:
> still! a php newbie.  This is kind of a touch base question.  I think
> I have it...
>
> I have an indexed array in which I progressively remove elements with
>
>     unset($my_array[$index]);
>
> This leave "holes", which I'm ok with.
>
> To now find the 5th element in this shot-up array I use
>
>     $my_array_piece =  array_splice($my_array, 5, 1);
>
> Is that how you "do" it?
>
> Unfortunately, the "indexes" have meaning.  I don't want to repack.

Oops... I mean array_splice($my_array, 4, 1) for the 5th element
Re: arrays with holes [message #176775 is a reply to message #176742] Mon, 23 January 2012 08:38 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 20/01/2012 21:44, cate escribió/wrote:
> On Jan 20, 2:42 pm, cate<catebekens...@yahoo.com> wrote:
>> still! a php newbie. This is kind of a touch base question. I think
>> I have it...
>>
>> I have an indexed array in which I progressively remove elements with
>>
>> unset($my_array[$index]);
>>
>> This leave "holes", which I'm ok with.
>>
>> To now find the 5th element in this shot-up array I use
>>
>> $my_array_piece = array_splice($my_array, 5, 1);
>>
>> Is that how you "do" it?
>>
>> Unfortunately, the "indexes" have meaning. I don't want to repack.
>
> Oops... I mean array_splice($my_array, 4, 1) for the 5th element

If array keys are meaningful but unknown, I can't figure out many other
ways to find for the nth element than array_splice(). But you need to
read the manual page carefully: your code, as is, will get rid of the
key. Check the forth parameter.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: arrays with holes [message #176780 is a reply to message #176775] Mon, 23 January 2012 22:59 Go to previous message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Álvaro G. Vicario)

> If array keys are meaningful but unknown, I can't figure out many other
> ways to find for the nth element than array_splice(). [...]

I think you could also use array_keys() and then get the nth key.
Something like this (untested and without any error checking):

function getElement(array $array, $index) {
$keys = array_keys($array);
return $array[$keys[$index]];
}

Wrap the whole thing into your own array class and you could even do

$foo = $array['foo'];
$bar = $array[5];

where the first statement would directly return the element with the key
'foo' and the second one the element at the 6th position, regardless of
the keys (using the function from above).

Just a quick idea.

Micha

--
http://mfesser.de/blickwinkel
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Parsing .css files with php: Cons?
Next Topic: Open Source Customization
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 15:11:03 GMT 2024

Total time taken to generate the page: 0.02413 seconds