Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6209

Re: How to construct an associative and numeric indexable array

Path csiph.com!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <9701294.O9o76ZdvQC@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Sun, 27 May 2012 19:48:11 +0200
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: How to construct an associative and numeric indexable array
Newsgroups comp.lang.php
References <jpp3vi$87f$1@dont-email.me> <jpp6u3$l9u$1@dont-email.me> <jpphes$6iq$1@dont-email.me> <jppkou$jug$1@dont-email.me> <jppm1b$p73$1@dont-email.me> <4od1s7lf3ach6e28sqh9gs5d02u7oqcpmj@mfesser.de> <jpqmkm$gil$1@dont-email.me> <1908618.nuHmqJ5PAb@PointedEars.de> <jps4d7$mcb$1@dont-email.me> <jptjp3$q79$1@dont-email.me>
Followup-To comp.lang.php
MIME-Version 1.0
Lines 132
NNTP-Posting-Date 27 May 2012 19:48:11 CEST
NNTP-Posting-Host b06ef430.newsspool3.arcor-online.net
X-Trace DXC=W]m4>@9b:\afF8a^:6>b7eMcF=Q^Z^V3h4Fo<]lROoRa8kF<OcfhCOke1h`^Sj[7onDZm8W4\YJNlJ`:FmdeL>K`fQ>EOE\[m`kICSfVAN5Iee
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.php:6209

Followups directed to: comp.lang.php

Show key headers only | View raw


Norman Peelman wrote:

> On 05/26/2012 10:44 PM, Leonardo Azpurua wrote:
>> "Thomas 'PointedEars' Lahn" [wrote]:
>>> Leonardo Azpurua wrote:
>>>> "Michael Fesser" [wrote]:
>>>> I just checked and in fact count($row) yields twice the number of
>>>> columns in the result set.
>>>>
>>>> And it is anything but appealing as a "general" solution.
>>>
>>> However, the array can also be created so that the value with the
>>> numeric key is a reference to the value with the non-numeric key, or
>>> vice-versa. Thereby the memory footprint of the array may be reduced; at
>>> least, inconsistencies are avoided:
>>>
>>> $ php -v
>>> PHP 5.3.10-1 (cli) (built: Feb  3 2012 10:03:01)
>>> Copyright (c) 1997-2012 The PHP Group
>>> Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
>>>     with XCache v1.3.2, Copyright (c) 2005-2011, by mOo
>>>     with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
>>>     with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
>>>
>>> $ phpsh -c
>>> php>  $a = array(); $a['foo'] = 'bar'; $a[0] =&  $a['foo']; print_r($a);
>>> Array
>>> (
>>>     [foo] =>  bar
>>>     [0] =>  bar
>>> )
>>>
>>> php>  $a[0] = 'baz'; print_r($a);
>>> Array
>>> (
>>>     [foo] =>  baz
>>>     [0] =>  baz
>>> )
>>
>> A nice solution, thanks!
> 
>    Solution to what?

See the Subject.  Of course, count($a) will still yield twice as much 
elements as there are real values stored in in $a, but at least the values 
are not simply duplicated, and they are synchronized.

An even more sophisticated approach that would account for the length would 
be a class whose constructor takes an array as parameter whose elements 
serve as elements for an encapsulated structure from which data can be 
retrieved both using the numeric and the non-numeric key, like so:

$ cat Map.php
<?php
  class Map
  {
    protected $_items = array();
    protected $_length = 0;

    public function __construct(array $items = array())
    {
      $this->_length = count($items);

      foreach (array_keys($items) as $key => $value)
      {
        $items[$key] =& $items[$value];
      }

      $this->_items =& $items;
    }

/*
The use of setters and getters would be limited by the fact that property 
names must be identifiers, so not numeric.  However, one could get used to 
prefixing numeric properties, e.g. with `_':
*/

    public function __get($name)
    {
      if (strpos($name, '_') === 0)
      {
        return $this->_items[substr($name, 1)];
      }

      return $this->{"_$name"};
    } 

    public function __set($name, $value)
    {
      if (strpos($name, '_') === 0)
      {
        $this->_items[substr($name, 1)] = $value;
      }
    } 
  }

  $m = new Map(array('foo' => 'bar'));
  $m->_0 = 'baz';
  echo print_r($m, true) . "\n";
  echo $m->length . "\n";

$ php -f Map.php 
Map Object
(
    [_items:protected] => Array
        (
            [foo] => baz
            [0] => baz
        )

    [_length:protected] => 1
)

1

>    Thomas created that array, not SQLSRV... what you
> are looking for is here:
> […]

Leonardo already stated several postings before:
| Ok... forget SQLSRV. It was just the source (not the object) of my 
| question.


HTH

PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to construct an associative and numeric indexable array "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-05-25 18:49 -0430
  Re: How to construct an associative and numeric indexable array Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-25 20:08 -0400
    Re: How to construct an associative and numeric indexable array "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-05-25 22:39 -0430
      Re: How to construct an associative and numeric indexable array Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-26 00:04 -0400
        Re: How to construct an associative and numeric indexable array "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-05-25 23:57 -0430
          Re: How to construct an associative and numeric indexable array Michael Fesser <netizen@gmx.de> - 2012-05-26 13:00 +0200
            Re: How to construct an associative and numeric indexable array "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-05-26 09:13 -0430
              Re: How to construct an associative and numeric indexable array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-27 00:31 +0200
                Re: How to construct an associative and numeric indexable array "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-05-26 22:14 -0430
                Re: How to construct an associative and numeric indexable array Norman Peelman <npeelmandog@cfl.rr.com> - 2012-05-27 12:12 -0400
                Re: How to construct an associative and numeric indexable array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-27 19:48 +0200
                Re: How to construct an associative and numeric indexable array Norman Peelman <npeelmandog@cfl.rr.com> - 2012-05-29 18:27 -0400
              Re: How to construct an associative and numeric indexable array Norman Peelman <npeelmandog@cfl.rr.com> - 2012-05-27 12:03 -0400

csiph-web