Associative Arrays

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
bhm8hwcm
Forum Newbie
Posts: 9
Joined: Mon May 05, 2003 10:53 am

Associative Arrays

Post by bhm8hwcm »

I have an associative array that I need to convert to a numeric based array. How would I do this.

eg.
$test = array('apple','orange','banana');

What I want to end up with is:
$newArray[0] being equal to apple
$newArray[1] being equal to orange
$newArray[2] being equal to banana

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$test = array('apple','orange','banana');

is the exact same thing as

$newArray[0] being equal to apple
$newArray[1] being equal to orange
$newArray[2] being equal to banana

Try:

Code: Select all

echo '<pre>';
print_r($test);
print_r($newArray);
BTW, your $test is not an associative array.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Re: Associative Arrays

Post by nickvd »

bhm8hwcm wrote:I have an associative array that I need to convert to a numeric based array. How would I do this.

eg.
$test = array('apple','orange','banana');

What I want to end up with is:
$newArray[0] being equal to apple
$newArray[1] being equal to orange
$newArray[2] being equal to banana

Thanks

Code: Select all

$newArray = $test;
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Or simply, $test[0], $test[1], $test[2]...
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Jcart wrote:BTW, your $test is not an associative array.
There are different types of arrays? Could you elaborate?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

An associative array can have indices with just about any character while a standard array has numeric indices

Standard

Code: Select all

$array = array('something', 'something else', 'something elser');
echo $array[1]; // outputs "something else"
Associative

Code: Select all

$array = array(
    'key' => 'Foo',
    'FOOEY_BAR' => 'Test',
    3 => 'hello'
);

echo $array['FOOEY_BAR']; // outputs "Test";
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Here is a quck and diry litle tutorial on arrays.


There are single (or standard) arrays, which do not contain association. That means that the keys in an array are numeric with no description at all.

ie:
$a = array('abc','123');

if we look at the structure of the array, it would be equivalent to us saying

Code: Select all

$a[0] = 'abc';
$a[1] = '123';
or

Code: Select all

$a[] = 'abc';
$a[] = '123';
if you notice, all we are doing is letting php do it's magic by saying "ok, let's build an array like we would an auto-incrementing database table, and place the next available integer as the key and the value whatever the user is requesting"



Now, an associative array is a little different. Again, I'll reference a mysql table here to show the importance and similarities.

Just like in a mysql table, we give descriptive names to fields in a table so that we know what kind of data goes in them.

ie, if we have a table called "users", we would probably have 3 fields: user_id, username, password

An associative array works the same way. Only we don't have to supply the TYPE for the value we are going to store like we would in a mysql table.

In our first example with the array $a, we gave it 2 array values, 'abc' and '123'. But we don't know what type of data that is, or what it's for because we have no descritive words or phrases to explain it. That is where an associative araray comes into play.

we'll recreate it now like so:

Code: Select all

$a = array('letters' => 'abc',
                   'numbers' => '123');
notice how we defined the array, and told it what to give it as a value.

so now, we could call the values back like so :

Code: Select all

echo $a['letters'];   // would echo out abc
echo $a['numbers'];  // would echo  out 123
we could also define it like this:

Code: Select all

$a['letters'] = 'abc';
$a['numbers'] = '123';
again, we could still call it back the same way.


Both arrays are the exact same. There is no difference at all except that our associative array is a bit more descriptive and easier to read than our normal, standard numeric array.


I hope that doesn't confuse you much and explains it a bit more. Arrays is a very large, extensive learning curve. It's a little hard to grasp at first, but once you start applying it yoursef and trying it out through testing, you'll get it in no time.

A good read is http://www.php.net/array

Go from there, and google "php array" to get more informatin.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

SidewinderX wrote:
Jcart wrote:BTW, your $test is not an associative array.
There are different types of arrays? Could you elaborate?
Most programming languages have two distinct types of "compound variables": arrays, where individual elements are identified by an ordinal number and "records" (or "hashes"), where elements are identified by name (also called "key"). As opposed to others, php has only one syntax construct used to represent both types. This construct is errorneously called "array" and documentation constantly mixes up arrays and hashes, what pretty much explains why php "arrays" are a big source of confusion for beginners. ;)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Associative Arrays

Post by Mordred »

bhm8hwcm wrote:I have an associative array that I need to convert to a numeric based array. How would I do this.

Code: Select all

$aNumeric = array_values($aAssociative);
Post Reply