Solved

Get value from associative array without key...

Posted on 2010-11-30
5
503 Views
Last Modified: 2012-05-10
Given an array like this, where I don't know the key name, how can I extract the value of both elements (AL, Alabama)?

Array
(
    [abbreviation] => AL
    [full_name] => Alabama
)



0
Comment
Question by:interclubs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Bruce Smith
ID: 34241258
you cannot unless you know the array variable's name in which case you would extract the values like $name['abbreviation']['full_name'];
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 34241613
If you mean that you don't know the key names (abbreviation, full_name) you can do something like this:

foreach ($array_name as $value){

     $x[] = $value;
}

That would give you an array of two elements in your example where $x[0] = 'AL' and $x[1] = 'Alabama'.

If you don't know the name of the actual array ($array_name) in my example, I have no idea how you would find the values.
0
 
LVL 1

Accepted Solution

by:
jebpotly earned 500 total points
ID: 34241967
array_keys will give you a list of the keys in an array.

If you have:

$my_array = array('abbreviation' => 'AL', 'full_name' => 'Alabama');

Open in new window


then you could use array_keys:
$my_keys = array_keys($my_array);

Open in new window


which would give you an array of Array ( [0] => abbreviation [1] => full_name ). You could then do
$first_value = $my_array[$my_keys[0]];
$second_value = $my_array[$my_keys[0]];

Open in new window


Or you could convert the array to use numeric indexes instead of string indexes. One way to do this is use the sort() method. So if you have the $my_array as defined above then you could do:
sort($my_array);

Open in new window

Now $my_array is Array ( [0] => Alabama [1] => AL) and you can use [0] and [1] to get the values.
$first_value = $my_array[0];
$second_value = $my_array[1];

Open in new window

0
 
LVL 11

Expert Comment

by:Bruce Smith
ID: 34243985
@jebpotly: Sweet, did not know about "array_keys($my_array);" Thanks
0
 
LVL 1

Expert Comment

by:jebpotly
ID: 34244028
;)
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question