Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create an array with multiple entries per value 1

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
I have an array which is populated by Codes and I use in a various queries and functions.

How can I include a second entry in the array so that I can also include a name, i.e.

I currently have:
Array = (ACC01,
ACC02,
ADV02)

I now want
Array = (ACC01 Accord Inc,
ACC02 Acctune Inc,
ADV02 Advanced Inc)

But I need the code and name seperate as the code is submitted later on.

Is my only option to put in some character like 'ACC01|Accord Inc' and then script to remove the character to process? Or is there a better, simpler way?
 
What you want is a multidimensional array. PHP tends to think of these kinds of things as arrays of arrays.

Something like:

$foo = array
(
array ('ACC01', 'Accord Inc.'),
array ('ACC02', 'Acctune Inc'),
array ('ADV02', 'Advanced Inc')
);

Will give you a multidimensional array with numerical subscripts. $foo[0][0] would be 'ACC01' and $foo[1][1] would be 'Acctune Inc'.

You could also use associative arrays:

$foo = array
(
array ('code' => 'ACC01', 'name' => 'Accord Inc.'),
array ('code' => 'ACC02', 'name' => 'Acctune Inc'),
array ('code' => 'ADV02', 'name' => 'Advanced Inc')
);

Thus $foo[0]['code'] would be 'ACC01' and $foo[1]['name'] would be "Acctune Inc'.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipinr, just what I needed to know!
 
You could also have an associative array like:
Code:
$foo = array ('ACC01' => 'Accord Inc.',
              'ACC02' => 'Acctune Inc',
              'ADV02' => 'Advanced Inc')
);

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top