[SOLVED] Advanced 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
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

[SOLVED] Advanced associative arrays

Post by pthomas »

Ok, I've got this table of items that are used throughout my current program. Currently, they are used from several different arrays and I want to combine it into an array of associative arrays and just have one array I reference. Here's what I'm using so far:

Code: Select all

$filenameї'Quality'] = "./files/quality.txt";
  $filenameї'Ending'] = "./files/ending.txt";
  $filenameї'So_Ugly'] = "./files/ugly.txt";
  $filenameї'So_Fat'] = "./files/fat.txt";
  $filenameї'So_Dumb'] = "./files/dumb.txt";
  $filenameї'Looked_Like'] = "./files/looked_like.txt";
  $filenameї'Misc'] = "./files/misc.txt";

  $DB_tableї'Quality'] = "adjective";
  $DB_tableї'Ending'] = "end_phrase";
  $DB_tableї'So_Ugly'] = "phrase";
  $DB_tableї'So_Fat'] = "phrase";
  $DB_tableї'So_Dumb'] = "phrase";
  $DB_tableї'Looked_Like'] = "phrase";
  $DB_tableї'Misc'] = "phrase";
  
  $typeї'Quality'] = $_POSTї'Quality'];
  $typeї'Ending'] = $_POSTї'Ending'];
  $typeї'So_Ugly'] = $_POSTї'So_Ugly'];
  $typeї'So_Fat'] = $_POSTї'So_Fat'];
  $typeї'So_Dumb'] = $_POSTї'So_Dumb'];
  $typeї'Looked_Like'] = $_POSTї'Looked_Like'];
  $typeї'Misc'] = $_POSTї'Misc'];
  
   $Table_names = array("Quality", "Ending", "So_Ugly", "So_Fat", 
   			"So_Dumb", "Looked_Like", "Misc");

Here's what I've got so far (which obviously isn't quite what I want). But it will be the array to end all arrays!

Code: Select all

*			Table Name	Column Name	Relative Path to file		Post variable ->Depriciated with $cat*/
   $cat = array(
		array("Quality"         , "adjective"	     , "./files/quality.txt"   , $_POSTї'Quality']),
		array("Ending"        , "end_phrase"	, "./files/ending.txt"	, $_POSTї'Ending']),
		array("So_Ugly"      , "phrase"	  , "./files/ugly.txt"	     , $_POSTї'So_Ugly']),
		array("So_Fat"        , "phrase"	   , "./files/fat.txt"	      , $_POSTї'So_Fat']),
		array("So_Dumb"    , "phrase"	        , "./files/dumb.txt"	, $_POSTї'So_Dumb']),
		array("Looked_like" , "phrase"	 , "./files/looked_like.txt" ,$_POSTї'Looked_Like']),
		array("Misc"            , "phrase"	     , "./files/misc.txt"		, $_POSTї'Misc'])
		 );
What I want to be able to do is specify something like

Code: Select all

$catї'Quality']ї'Column_name'] and get "adjective"
or call the array this way

Code: Select all

$catї'So_Fat']ї'Path'] and get "./files/fat.txt"
Any ideas on how to set this up? The big picture is that each item represents a category and its corresponding spot in a MySQL database along with a few other attributes. I'm trying to keep my programming as dynamic as possible so that if we want to add a category later on, we add a line in the array in one file and it works globally.

Thanks,
Paul
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Associative arrays are easy once you get your head wrapped around it. The simplest way to do it would be:

Code: Select all

$omega_arrayї'path'] = $filename;
$omega_arrayї'Column_name'] = $DB_table;
$omega_arrayї'type'] = $type;
Doing that, you will be able to call:
$omega_array['Column_name']['Quality'], and get "adjective"

Otherwise, set it up like this:
$cat = array(['Quality']=>array('Column_name'=>'adjective','Path'=>'./files/quality.txt' .... and so on
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Thanks, that was what I needed to see to get going. I ended up using

Code: Select all

$DATA = array(	'Quality'	=>array('Common_Name'=>'Quality'	,'DB_Table'=>'Quality'		,'Table_Col'=>'adjective'	,'File'=>'./files/quality.txt'),
		'Ending'	=>array('Common_Name'=>'Ending'		,'DB_Table'=>'Ending'		,'Table_Col'=>'end_phrase'	,'File'=>'./files/ending.txt'),
		'So_Ugly'	=>array('Common_Name'=>'So Ugly'	,'DB_Table'=>'So_Ugly'		,'Table_Col'=>'phrase'		,'File'=>'./files/ugly.txt'),
		'So_Fat'	=>array('Common_Name'=>'So Fat'		,'DB_Table'=>'So_Fat'		,'Table_Col'=>'phrase'		,'File'=>'./files/fat.txt'),
		'So_Dumb'	=>array('Common_Name'=>'So Dumb'	,'DB_Table'=>'So_Dumb'		,'Table_Col'=>'phrase'		,'File'=>'./files/dumb.txt'),
		'Looked_Like'	=>array('Common_Name'=>'Looked Like'	,'DB_Table'=>'Looked_Like'	,'Table_Col'=>'phrase'		,'File'=>'./files/looked_like.txt'),
		'Misc'		=>array('Common_Name'=>'Misc.'		,'DB_Table'=>'Misc'		,'Table_Col'=>'phrase'		,'File'=>'./files/misc.txt')
	);
And its working great and I'm understanding it too..Looked alot better before this page formatted it!
Paul
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

One more question about them. How would I loop through one of the internal arrays in the $DATA array that I crated above?

I tried

Code: Select all

($DATA as $categoryї'DB_Table'] => $name)
but that didn;t work. Any ideas on how to just loop through the 'DB_Table' items? I'm lost.

Paul
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

foreach( $DATA as $key => $value )
{
  echo $key . " = " . var_export($value,true);
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

foreach($DATA as $row => $vals)
{
      // $row will be quality, ending, ...
      // $vals is the array you add to quality

     echo $valsї'DB_Table'];
}
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Yup, turned out I was going about it all wrong. And while messing with it I figured it out and then got a notice in my e-mail almost immediately after. You guys are quick.

Thanks again for the help,
Paul
Post Reply