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 #178286 is a reply to message #178279] Sun, 27 May 2012 16:03 Go to previous message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma:
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?-
[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: Thu Nov 28 07:04:13 GMT 2024

Total time taken to generate the page: 0.04149 seconds