Norman Peelman wrote:
> On 05/26/2012 10:44 PM, Leonardo Azpurua wrote:
>> "Thomas 'PointedEars' Lahn" [wrote]:
>>> Leonardo Azpurua wrote:
>>>> "Michael Fesser" [wrote]:
> 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