Page 1 of 1
Getting a value in an associate array
Posted: Mon Dec 31, 2012 4:08 pm
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" )
);
Re: Getting a value in an associate array
Posted: Mon Dec 31, 2012 5:28 pm
by requinix
"Wireless" is the array key and the third element will be at index 2.
Re: Getting a value in an associate array
Posted: Mon Dec 31, 2012 10:49 pm
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
Re: Getting a value in an associate array
Posted: Tue Jan 01, 2013 9:29 am
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
Re: Getting a value in an associate array
Posted: Tue Jan 01, 2013 4:40 pm
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.
Re: Getting a value in an associate array
Posted: Tue Jan 01, 2013 6:36 pm
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.
Re: Getting a value in an associate array
Posted: Wed Jan 02, 2013 9:25 pm
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?
Re: Getting a value in an associate array
Posted: Mon Jan 07, 2013 5:09 pm
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.