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
Switch to threaded view of this topic Create a new topic Submit Reply
How to construct an associative and numeric indexable array [message #178270] Fri, 25 May 2012 23:19 Go to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
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?

--
Re: How to construct an associative and numeric indexable array [message #178272 is a reply to message #178270] Sat, 26 May 2012 00:08 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: How to construct an associative and numeric indexable array [message #178274 is a reply to message #178272] Sat, 26 May 2012 03:09 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"Jerry Stuckle" <jstucklex(at)attglobal(dot)net> escribió en el mensaje
news:jpp6u3$l9u$1(at)dont-email(dot)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!
--
Re: How to construct an associative and numeric indexable array [message #178276 is a reply to message #178274] Sat, 26 May 2012 04:04 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/25/2012 11:09 PM, Leonardo Azpurua wrote:
> "Jerry Stuckle"<jstucklex(at)attglobal(dot)net> escribió en el mensaje
> news:jpp6u3$l9u$1(at)dont-email(dot)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!
> --
>
>

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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: How to construct an associative and numeric indexable array [message #178277 is a reply to message #178276] Sat, 26 May 2012 04:27 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"Jerry Stuckle" <jstucklex(at)attglobal(dot)net> escribió en el mensaje
news:jppkou$jug$1(at)dont-email(dot)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!

--
Re: How to construct an associative and numeric indexable array [message #178278 is a reply to message #178277] Sat, 26 May 2012 11:00 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.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
Re: How to construct an associative and numeric indexable array [message #178279 is a reply to message #178278] Sat, 26 May 2012 13:43 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"Michael Fesser" <netizen(at)gmx(dot)de> escribió en el mensaje
news:4od1s7lf3ach6e28sqh9gs5d02u7oqcpmj(at)mfesser(dot)de...
> .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.

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!

--
Re: How to construct an associative and numeric indexable array [message #178282 is a reply to message #178279] Sat, 26 May 2012 22:31 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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
Re: How to construct an associative and numeric indexable array [message #178285 is a reply to message #178282] Sun, 27 May 2012 02:44 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"Thomas 'PointedEars' Lahn" <PointedEars(at)web(dot)de> escribió en el mensaje
news:1908618(dot)nuHmqJ5PAb(at)PointedEars(dot)de...
> 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
> )

A nice solution, thanks!

--
Re: How to construct an associative and numeric indexable array [message #178286 is a reply to message #178279] Sun, 27 May 2012 16:03 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 05/26/2012 09:43 AM, Leonardo Azpurua wrote:
> "Michael Fesser"<netizen(at)gmx(dot)de> escribió en el mensaje
> news:4od1s7lf3ach6e28sqh9gs5d02u7oqcpmj(at)mfesser(dot)de...
>> .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.
>
> 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!
>

At which point you would have stumbled upon the answer. :)

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: How to construct an associative and numeric indexable array [message #178287 is a reply to message #178285] Sun, 27 May 2012 16:12 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 05/26/2012 10:44 PM, Leonardo Azpurua wrote:
> "Thomas 'PointedEars' Lahn"<PointedEars(at)web(dot)de> escribió en el mensaje
> news:1908618(dot)nuHmqJ5PAb(at)PointedEars(dot)de...
>> 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
>> )
>
> A nice solution, thanks!
>
> --
>
>

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



--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
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 next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
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
Re: How to construct an associative and numeric indexable array [message #178297 is a reply to message #178288] Tue, 29 May 2012 22:27 Go to previous message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
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


--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Fri Nov 22 02:22:18 GMT 2024

Total time taken to generate the page: 0.02790 seconds