Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to construct an associative and numeric indexable array

5 views
Skip to first unread message

Leonardo Azpurua

unread,
May 25, 2012, 7:19:08 PM5/25/12
to
Hi,

The docs for MS's SQLSRV PHP interface sayes:

"Based on the value of the $fetchType parameter, the returned array can be a
numerically indexed array, an associative array, or both."

It might be helpful to have such an array in some other places.

How is it done?

--


Jerry Stuckle

unread,
May 25, 2012, 8:08:29 PM5/25/12
to
RTFM:

http://us.php.net/manual/en/function.mssql-fetch-array.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Leonardo Azpurua

unread,
May 25, 2012, 11:09:10 PM5/25/12
to

"Jerry Stuckle" <jstu...@attglobal.net> escribió en el mensaje
news:jpp6u3$l9u$1...@dont-email.me...
> On 5/25/2012 7:19 PM, Leonardo Azpurua wrote:
>> Hi,
>>
>> The docs for MS's SQLSRV PHP interface sayes:
>>
>> "Based on the value of the $fetchType parameter, the returned array can
>> be a
>> numerically indexed array, an associative array, or both."
>>
>> It might be helpful to have such an array in some other places.
>>
>> How is it done?
>>
>
> RTFM:
>
> http://us.php.net/manual/en/function.mssql-fetch-array.php

Hi,

As far as I have RdTFM, an array can be indexed either by key values
$a1 = array("L1" => "a", "L2" => "b");
or by an integer
$a2 = array("a", "b");

You can write $a1["L1"], or $a2[0].

Of course, there is no point in writing $a2["L1"], but being able to write
$a1[0] might certainly come handy at times.

My mention of the SQLSRV/PHP Driver (not to be confused with the "old" mssql
library) was because it provides the function sqlsrv_fetch_array, that
returns a row from a data source as an array whose elements may be accessed
both by their column name or by their ordinal position (minus one) in the
column list of the query that generated the statement resource.

So, given a statement ($stmt) based on the query SELECT C1, C2 FROM T, and

$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_BOTH);

both $row["C1"] and $row[0] are acceptable and equivalent.

My question is *not* how to fetch rows from a result set, but how can I
build an array that allows both ways of indexing.

I know that if I RTFM, think a lot and devote a few hours (or even days) to
the problem, I might come with some sort of solution.

But it doesn't hurt to ask, just in case somebody has an answer.

Better if it is an answer to the actual question, but everything helps.


Thanks!
--


Jerry Stuckle

unread,
May 26, 2012, 12:04:38 AM5/26/12
to
OK, you're using the newer functions. Still, RTFM.

http://us3.php.net/manual/en/function.sqlsrv-fetch-array.php

It explains things quite clearly. But if you don't know PHP, then you
need a lot more help than this page.

Leonardo Azpurua

unread,
May 26, 2012, 12:27:18 AM5/26/12
to

"Jerry Stuckle" <jstu...@attglobal.net> escribió en el mensaje
news:jppkou$jug$1...@dont-email.me...
>
> OK, you're using the newer functions. Still, RTFM.
>
> http://us3.php.net/manual/en/function.sqlsrv-fetch-array.php
>
> It explains things quite clearly. But if you don't know PHP, then you
> need a lot more help than this page.

Hi, Jerry:

Ok... forget SQLSRV. It was just the source (not the object) of my question.

Let's say I have an array $arr, constructed thus:

$arr = array("one" => "X", "two"=>"Y", ..., "umpt" =>"ZZZZZ");

so that I can get $arr["one"] or $arr["umpt"].

Now, at some moment, I want to obtain the (n+1)th value in the array. I just
can't write $arr[$n];

I know I might write:
$k = array_keys($arr);
$x = $arr[$k[$n]];

But when you use the array returned by sqlsrv_fetch_array, you can either
write $arr["C1"] or $arr[0].

Do you know how they do that?

Thanks!

--


Michael Fesser

unread,
May 26, 2012, 7:00:32 AM5/26/12
to
.oO(Leonardo Azpurua)

>Ok... forget SQLSRV. It was just the source (not the object) of my question.
>
>Let's say I have an array $arr, constructed thus:
>
> $arr = array("one" => "X", "two"=>"Y", ..., "umpt" =>"ZZZZZ");
>
>so that I can get $arr["one"] or $arr["umpt"].
>
>Now, at some moment, I want to obtain the (n+1)th value in the array. I just
>can't write $arr[$n];
>
>I know I might write:
> $k = array_keys($arr);
> $x = $arr[$k[$n]];
>
>But when you use the array returned by sqlsrv_fetch_array, you can either
>write $arr["C1"] or $arr[0].
>
>Do you know how they do that?

Such an array contains all values twice. There's no direct way to access
an element with an associative key also by a numeric index. For doing
that you would have to loop through the array until you've reached the
element you want.

Micha

--
http://mfesser.de/blickwinkel

Leonardo Azpurua

unread,
May 26, 2012, 9:43:28 AM5/26/12
to

"Michael Fesser" <net...@gmx.de> escribió en el mensaje
news:4od1s7lf3ach6e28s...@mfesser.de...
So that's how they do it!

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.

You also saved me from stumbling against "for ($n = 0; $n < count($row);
$n++)", which would have happened sooner than later.

Thanks!

--


Thomas 'PointedEars' Lahn

unread,
May 26, 2012, 6:31:31 PM5/26/12
to
Leonardo Azpurua wrote:

> "Michael Fesser" [wrote]:
>> .oO(Leonardo Azpurua)
>>> But when you use the array returned by sqlsrv_fetch_array, you can
>>> either write $arr["C1"] or $arr[0].
>>> Do you know how they do that?
>>
>> Such an array contains all values twice. There's no direct way to access
>> an element with an associative key also by a numeric index. For doing
>> that you would have to loop through the array until you've reached the
>> element you want.
>
> So that's how they do it!
>
> 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
)


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

Leonardo Azpurua

unread,
May 26, 2012, 10:44:52 PM5/26/12
to

"Thomas 'PointedEars' Lahn" <Point...@web.de> escribió en el mensaje
news:1908618.n...@PointedEars.de...
A nice solution, thanks!

--


Norman Peelman

unread,
May 27, 2012, 12:03:09 PM5/27/12
to
At which point you would have stumbled upon the answer. :)

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Norman Peelman

unread,
May 27, 2012, 12:12:18 PM5/27/12
to
Solution to what? Thomas created that array, not SQLSRV... what you
are looking for is here:

http://us.php.net/manual/en/sqlsrv.constants.php

... the top three items are of interest.

and examples:

http://us.php.net/manual/en/function.sqlsrv-fetch-array.php

Thomas 'PointedEars' Lahn

unread,
May 27, 2012, 1:48:11 PM5/27/12
to
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

Norman Peelman

unread,
May 29, 2012, 6:27:53 PM5/29/12
to
On 05/27/2012 01:48 PM, Thomas 'PointedEars' Lahn wrote:
> 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.
>>>>>

Looks like I misread the question. oops!

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


--
0 new messages