Getting a value in an associate array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Getting a value in an associate array

Post by Lonestarjack »

I have an associate array and need to get the value of the 3rd element of "wireless".

Code: Select all

$arr =array
(
"City"             => Array  (  12, 29, "Y"   ),
"Atmos"          => Array  (   8, 23, "Y"   ),
"Verizon"         => Array  (   6, 22, "Y"   ),
"Wireless"       => Array  (  28, 14, "Y"   ),
"Payne"           => Array  (   1, 14, "N"   )
);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Getting a value in an associate array

Post by requinix »

"Wireless" is the array key and the third element will be at index 2.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Getting a value in an associate array

Post by twinedev »

$arry['Wireless'][2]

One way to see it mapped our is to do a print_r($arr); (either wrap it with <pre><tt><?php print_r($arr); ?></tt></pre> tags or do View Source to see it formatted nicely).

Print_r is a little more neater than var_dump when you know it is only arrays.

-Greg
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Getting a value in an associate array

Post by Lonestarjack »

I had tried the print_r etc, and looked at the array, but when I tried to access an element nothing happened.
Seems that I was using mysql_fetch_assoc($result) and I couldn't get to an enumerated element, however I could with mysql_fetch_row($result).
I was under the impression that the mysql_fetch_assoc($result) gave both types of access.
Now I see that the favored method of fetching a record is to use the mysqli api.
Thanks for you help
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Getting a value in an associate array

Post by Christopher »

Lonestarjack wrote:I had tried the print_r etc, and looked at the array, but when I tried to access an element nothing happened.
When you say "nothing happened" do you mean that nothing was displayed? Did you check to see if what you were accessing isset()?
Lonestarjack wrote:Seems that I was using mysql_fetch_assoc($result) and I couldn't get to an enumerated element, however I could with mysql_fetch_row($result).
I was under the impression that the mysql_fetch_assoc($result) gave both types of access.
I would recommend having an associative array returned. Then if you insert a column in the table schema your code does not break.
Lonestarjack wrote:Now I see that the favored method of fetching a record is to use the mysqli api.
Yes, mysqli or PDO.
(#10850)
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Getting a value in an associate array

Post by Lonestarjack »

1, Nothing displayed.
2. I normally use the associative, but in this case I wanted to use the enumerative method.
3. Can always add it on the end.
The page in question has been working well for the past 5 years. It is a large hard coded table that I would rather drive by an array and loops.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Getting a value in an associate array

Post by Christopher »

Lonestarjack wrote:1, Nothing displayed.
So did you check if the value isset()? Did you dump the array with print_r() or var_dump() to see what it actually contains?
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Getting a value in an associate array

Post by pickle »

Lonestarjack wrote:2. I normally use the associative, but in this case I wanted to use the enumerative method.
Just FYI - you can iterate over an associative array by using foreach(), if looping was your reason for using a numbered array.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply