Associative Arrays
Moderator: General Moderators
Associative Arrays
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
$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:
BTW, your $test is not an associative array.
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);-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
Re: Associative Arrays
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;-
SidewinderX
- Forum Contributor
- Posts: 407
- Joined: Fri Jul 16, 2004 9:04 pm
- Location: NY
An associative array can have indices with just about any character while a standard array has numeric indices
Standard
Associative
Standard
Code: Select all
$array = array('something', 'something else', 'something elser');
echo $array[1]; // outputs "something else"Code: Select all
$array = array(
'key' => 'Foo',
'FOOEY_BAR' => 'Test',
3 => 'hello'
);
echo $array['FOOEY_BAR']; // outputs "Test";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
or
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:
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 :
we could also define it like this:
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.
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';Code: Select all
$a[] = 'abc';
$a[] = '123';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');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 123Code: Select all
$a['letters'] = 'abc';
$a['numbers'] = '123';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.
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
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.SidewinderX wrote:There are different types of arrays? Could you elaborate?Jcart wrote:BTW, your $test is not an associative array.
Re: Associative Arrays
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);