Page 1 of 1

Associative Arrays

Posted: Sat Mar 24, 2007 3:08 pm
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

Posted: Sat Mar 24, 2007 3:10 pm
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.

Re: Associative Arrays

Posted: Sat Mar 24, 2007 3:47 pm
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;

Posted: Sun Mar 25, 2007 8:31 am
by aaronhall
Or simply, $test[0], $test[1], $test[2]...

Posted: Sun Mar 25, 2007 7:28 pm
by SidewinderX
Jcart wrote:BTW, your $test is not an associative array.
There are different types of arrays? Could you elaborate?

Posted: Sun Mar 25, 2007 11:01 pm
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";

Posted: Sun Mar 25, 2007 11:48 pm
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.

Posted: Mon Mar 26, 2007 4:14 am
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. ;)

Re: Associative Arrays

Posted: Mon Mar 26, 2007 2:47 pm
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);