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

Home » Imported messages » comp.lang.php » unset multidimensional array element
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
unset multidimensional array element [message #180526] Sun, 24 February 2013 00:18 Go to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
Is this correct?

I remove an element from a multidee array. Numeric Indexing then
fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
unset. It seems the a numeric index has graduated to a key. (I
think I read that's the way php works)

Do I have to repack this to get sequential indexing to work or is
there a php way to remove elements and maintain a "numeric" indexs?

I suppose I could make it a rule to use the construct foreach, but I
do so love for(;;).

Thank you in advance.


array (
array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9)
)
Re: unset multidimensional array element [message #180527 is a reply to message #180526] Sun, 24 February 2013 01:17 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/23/2013 7:18 PM, cate wrote:
> Is this correct?
>
> I remove an element from a multidee array. Numeric Indexing then
> fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
> unset. It seems the a numeric index has graduated to a key. (I
> think I read that's the way php works)
>
> Do I have to repack this to get sequential indexing to work or is
> there a php way to remove elements and maintain a "numeric" indexs?
>
> I suppose I could make it a rule to use the construct foreach, but I
> do so love for(;;).
>
> Thank you in advance.
>
>
> array (
> array (1, 2, 3),
> array (4, 5, 6),
> array (7, 8, 9)
> )
>

All arrays in PHP are associative (hashes). If you remove an element,
that key no longer exists.

You can renumber the array, or you can use operators like foreach(),
current(), next(), etc. and not worry about the keys.

Very seldom do I need to care about the array indexes being in a
specific order.
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: unset multidimensional array element [message #180528 is a reply to message #180527] Sun, 24 February 2013 01:19 Go to previous messageGo to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
On Feb 23, 7:17 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 2/23/2013 7:18 PM, cate wrote:
>
>
>
>
>
>
>
>
>
>> Is this correct?
>
>> I remove an element from a multidee array.  Numeric Indexing then
>> fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
>> unset.   It seems the a numeric index has graduated to a key.  (I
>> think I read that's the way php works)
>
>> Do I have to repack this to get sequential indexing to work or is
>> there a php way to remove elements and maintain a "numeric" indexs?
>
>> I suppose I could make it a rule to use the construct foreach, but I
>> do so love for(;;).
>
>> Thank you in advance.
>
>> array (
>>     array (1, 2, 3),
>>     array (4, 5, 6),
>>     array (7, 8, 9)
>> )
>
> All arrays in PHP are associative (hashes).  If you remove an element,
> that key no longer exists.
>
> You can renumber the array, or you can use operators like foreach(),
> current(), next(), etc. and not worry about the keys.
>
> Very seldom do I need to care about the array indexes being in a
> specific order.
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

I don't think there is any alternative in php. Thank you.
Re: unset multidimensional array element [message #180529 is a reply to message #180528] Sun, 24 February 2013 01:21 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/23/2013 8:19 PM, cate wrote:
> On Feb 23, 7:17 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 2/23/2013 7:18 PM, cate wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> Is this correct?
>>
>>> I remove an element from a multidee array. Numeric Indexing then
>>> fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
>>> unset. It seems the a numeric index has graduated to a key. (I
>>> think I read that's the way php works)
>>
>>> Do I have to repack this to get sequential indexing to work or is
>>> there a php way to remove elements and maintain a "numeric" indexs?
>>
>>> I suppose I could make it a rule to use the construct foreach, but I
>>> do so love for(;;).
>>
>>> Thank you in advance.
>>
>>> array (
>>> array (1, 2, 3),
>>> array (4, 5, 6),
>>> array (7, 8, 9)
>>> )
>>
>> All arrays in PHP are associative (hashes). If you remove an element,
>> that key no longer exists.
>>
>> You can renumber the array, or you can use operators like foreach(),
>> current(), next(), etc. and not worry about the keys.
>>
>> Very seldom do I need to care about the array indexes being in a
>> specific order.
>
> I don't think there is any alternative in php. Thank you.
>

As I said - *in PHP* I very seldom need to care about the array indexes
being in a specific order. There are many ways of doing things with
arrays - and you don't even need a numeric index.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: unset multidimensional array element [message #180530 is a reply to message #180529] Sun, 24 February 2013 01:24 Go to previous messageGo to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
On Feb 23, 7:21 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 2/23/2013 8:19 PM, cate wrote:
>
>
>
>
>
>
>
>
>
>> On Feb 23, 7:17 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>>> On 2/23/2013 7:18 PM, cate wrote:
>
>>>> Is this correct?
>
>>>> I remove an element from a multidee array.  Numeric Indexing then
>>>> fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
>>>> unset.   It seems the a numeric index has graduated to a key.  (I
>>>> think I read that's the way php works)
>
>>>> Do I have to repack this to get sequential indexing to work or is
>>>> there a php way to remove elements and maintain a "numeric" indexs?
>
>>>> I suppose I could make it a rule to use the construct foreach, but I
>>>> do so love for(;;).
>
>>>> Thank you in advance.
>
>>>> array (
>>>>      array (1, 2, 3),
>>>>      array (4, 5, 6),
>>>>      array (7, 8, 9)
>>>> )
>
>>> All arrays in PHP are associative (hashes).  If you remove an element,
>>> that key no longer exists.
>
>>> You can renumber the array, or you can use operators like foreach(),
>>> current(), next(), etc. and not worry about the keys.
>
>>> Very seldom do I need to care about the array indexes being in a
>>> specific order.
>
>> I don't think there is any alternative in php.  Thank you.
>
> As I said - *in PHP* I very seldom need to care about the array indexes
> being in a specific order.  There are many ways of doing things with
> arrays - and you don't even need a numeric index.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Never an need for order (mumeric) is quite a statement. :-)
Re: unset multidimensional array element [message #180531 is a reply to message #180530] Sun, 24 February 2013 01:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/23/2013 8:24 PM, cate wrote:
> On Feb 23, 7:21 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 2/23/2013 8:19 PM, cate wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> On Feb 23, 7:17 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>>>> On 2/23/2013 7:18 PM, cate wrote:
>>
>>>> > Is this correct?
>>
>>>> > I remove an element from a multidee array. Numeric Indexing then
>>>> > fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
>>>> > unset. It seems the a numeric index has graduated to a key. (I
>>>> > think I read that's the way php works)
>>
>>>> > Do I have to repack this to get sequential indexing to work or is
>>>> > there a php way to remove elements and maintain a "numeric" indexs?
>>
>>>> > I suppose I could make it a rule to use the construct foreach, but I
>>>> > do so love for(;;).
>>
>>>> > Thank you in advance.
>>
>>>> > array (
>>>> > array (1, 2, 3),
>>>> > array (4, 5, 6),
>>>> > array (7, 8, 9)
>>>> > )
>>
>>>> All arrays in PHP are associative (hashes). If you remove an element,
>>>> that key no longer exists.
>>
>>>> You can renumber the array, or you can use operators like foreach(),
>>>> current(), next(), etc. and not worry about the keys.
>>
>>>> Very seldom do I need to care about the array indexes being in a
>>>> specific order.
>>
>>> I don't think there is any alternative in php. Thank you.
>>
>> As I said - *in PHP* I very seldom need to care about the array indexes
>> being in a specific order. There are many ways of doing things with
>> arrays - and you don't even need a numeric index.
>>
>
> Never an need for order (mumeric) is quite a statement. :-)
>

First of all, I said "VERY SELDOM" - not never. There are exceptions.

It's all about knowing the language and using the appropriate features.
It works quite well.

P.S. Please learn to trim signatures. They shouldn't be copied. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: unset multidimensional array element [message #180532 is a reply to message #180531] Sun, 24 February 2013 01:40 Go to previous messageGo to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
On Feb 23, 7:34 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 2/23/2013 8:24 PM, cate wrote:
>
>
>
>
>
>
>
>
>
>> On Feb 23, 7:21 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>>> On 2/23/2013 8:19 PM, cate wrote:
>
>>>> On Feb 23, 7:17 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>>>> > On 2/23/2013 7:18 PM, cate wrote:
>
>>>> >> Is this correct?
>
>>>> >> I remove an element from a multidee array.  Numeric Indexing then
>>>> >> fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
>>>> >> unset.   It seems the a numeric index has graduated to a key.  (I
>>>> >> think I read that's the way php works)
>
>>>> >> Do I have to repack this to get sequential indexing to work or is
>>>> >> there a php way to remove elements and maintain a "numeric" indexs?
>
>>>> >> I suppose I could make it a rule to use the construct foreach, but I
>>>> >> do so love for(;;).
>
>>>> >> Thank you in advance.
>
>>>> >> array (
>>>> >>       array (1, 2, 3),
>>>> >>       array (4, 5, 6),
>>>> >>       array (7, 8, 9)
>>>> >> )
>
>>>> > All arrays in PHP are associative (hashes).  If you remove an element,
>>>> > that key no longer exists.
>
>>>> > You can renumber the array, or you can use operators like foreach(),
>>>> > current(), next(), etc. and not worry about the keys.
>
>>>> > Very seldom do I need to care about the array indexes being in a
>>>> > specific order.
>
>>>> I don't think there is any alternative in php.  Thank you.
>
>>> As I said - *in PHP* I very seldom need to care about the array indexes
>>> being in a specific order.  There are many ways of doing things with
>>> arrays - and you don't even need a numeric index.
>
>> Never an need for order (mumeric) is quite a statement.  :-)
>
> First of all, I said "VERY SELDOM" - not never.  There are exceptions.
>
> It's all about knowing the language and using the appropriate features.
>   It works quite well.
>
> P.S. Please learn to trim signatures.  They shouldn't be copied.  Thanks.

lol - It was my CNN mode. Thanks again.
Re: unset multidimensional array element [message #180533 is a reply to message #180526] Sun, 24 February 2013 13:40 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 24.02.2013 01:18, schrieb cate:
> Is this correct?
>
> I remove an element from a multidee array. Numeric Indexing then
> fails because the it's no longer 0, 1, 2 but instead 0, 2 after the
> unset. It seems the a numeric index has graduated to a key. (I
> think I read that's the way php works)
>
> Do I have to repack this to get sequential indexing to work or is
> there a php way to remove elements and maintain a "numeric" indexs?
>
> I suppose I could make it a rule to use the construct foreach, but I
> do so love for(;;).
>
> Thank you in advance.
>
>
> array (
> array (1, 2, 3),
> array (4, 5, 6),
> array (7, 8, 9)
> )
>
This is the behaviour PHP programmers IMO like. Imagine you have a unique database
key as index, I would not like it to be renumbered after deletion of one entry.

/Str.
Re: unset multidimensional array element [message #180534 is a reply to message #180533] Sun, 24 February 2013 15:36 Go to previous messageGo to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
> This is the behaviour PHP programmers IMO like. Imagine you have a unique database
>
> key as index, I would not like it to be renumbered after deletion of one entry.
>
>
>
> /Str.

Me neither ... :-)

$a = array(1,2,3,4,5,9,8,7,6);
unset($a[4]);
print_r($a);
$ar = array_reverse($a);
print_r($ar);
Re: unset multidimensional array element [message #180535 is a reply to message #180534] Sun, 24 February 2013 16:25 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 24.02.2013 16:36, schrieb catebekensail(at)yahoo(dot)com:
>
>> This is the behaviour PHP programmers IMO like. Imagine you have a unique database
>>
>> key as index, I would not like it to be renumbered after deletion of one entry.
>>
>>
>>
>> /Str.
>
> Me neither ... :-)
>
> $a = array(1,2,3,4,5,9,8,7,6);
> unset($a[4]);
> print_r($a);

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[5] => 9
[6] => 8
[7] => 7
[8] => 6
)
> $ar = array_reverse($a);
> print_r($ar);

But:
$a = array(1=>'one',2=>'two',3=>'three',4=>'four');
unset($a[2]);
print_r($a);
Array
(
[1] => one
[3] => three
[4] => four
)

It must be difficult to understand for the beginner, but in fact "it just works" if
you set the index.

I counted 47 array functions with "array" in the name, and about the same number with
other names (http://php.net/manual/en/function.array.php), most of them I find fairly
unusable.

/Str.
Re: unset multidimensional array element [message #180541 is a reply to message #180534] Mon, 25 February 2013 12:01 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Feb 24, 3:36 pm, catebekens...@yahoo.com wrote:
>> This is the behaviour PHP programmers IMO like. Imagine you have a unique database
>
>> key as index, I would not like it to be renumbered after deletion of one entry.
>
>> /Str.
>
> Me neither ... :-)
>
> $a = array(1,2,3,4,5,9,8,7,6);
> unset($a[4]);
> print_r($a);
> $ar = array_reverse($a);
> print_r($ar);

If you want to renumber your array without changing the natural order
then use array_merge($a), rather than array_reverse($a). But like the
others here, I urge you to think in terms of collections (where
foreach operates), rather than numeric array indexes.
Re: unset multidimensional array element [message #180542 is a reply to message #180534] Mon, 25 February 2013 16:13 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/24/2013 10:36 AM, catebekensail(at)yahoo(dot)com wrote:
>
>> This is the behaviour PHP programmers IMO like. Imagine you have a unique database
>>
>> key as index, I would not like it to be renumbered after deletion of one entry.
>>
>>
>>
>> /Str.
>
> Me neither ... :-)
>
> $a = array(1,2,3,4,5,9,8,7,6);
> unset($a[4]);
> print_r($a);
> $ar = array_reverse($a);
> print_r($ar);
>

Which renumbers the indexes in the array.

The real question here is - WHY do you think you need the keys renumbered?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: preg_replace... I can't do it!!
Next Topic: simpletest vs phpunit vs ...
Goto Forum:
  

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

Current Time: Sat Nov 30 20:05:52 GMT 2024

Total time taken to generate the page: 0.02452 seconds