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

Home » Imported messages » comp.lang.php » How to construct an associative and numeric indexable array
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: How to construct an associative and numeric indexable array [message #178288 is a reply to message #178287] Sun, 27 May 2012 17:48 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Overriding PHP INI Setting session.use_trans_sid=0
Next Topic: Saving a three-dimensional array to the mysql database.
Goto Forum:
  

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

Current Time: Sat Sep 28 22:56:56 GMT 2024

Total time taken to generate the page: 0.04224 seconds