I am working on a module that uses a function to load a multi-dimension array's values

// Function to load all plan data into multi-dimension array
function bible_plans() {
	$plans = array();
	$sql = 'SELECT * FROM {bibleplans_plans}';
	$result = db_query(db_rewrite_sql($sql));
		while ($plan = db_fetch_object($result)) {
			$plans[$plan->nid] = array(
 				'name' 	=> $plan->name,
 				'description'=> $plan->description,
			);
		}
	return $plans;
}

To build an array for specific functions, like a menu item's options, I iterate through the values of the key I need. It's only a few lines of code but it seems like there is an easier way, or a function I'm not aware of. How should I do this?

$plan_info = bible_plans(); // Load multi-dimension array from above function
        // Iterate through array and build new array of just the $plan names
	foreach ($plan_info as $plan){ 
		$plans[] = $plan['name'];
	}

Comments

pedrofaria’s picture

Sorry... there isn't another better way to do it...

MAYBE you can reduce your bible_plans function...


// Function to load all plan data into multi-dimension array
function bible_plans() {
    $plans = array();
    $sql = 'SELECT nid, name, description FROM {bibleplans_plans}';
    $result = db_query(db_rewrite_sql($sql));
        while ($plan = db_fetch_array($result)) {
            $plans[$plan['nid']] = $plan;
        }
    return $plans;
}

:) 3 lines

cya

--
Pedro Faria de Mirando Pinto
http://www.phpavancado.net - DevBlog (pt_BR)
http://www.eusouopedro.com - Blog (pt_BR)
irc://irc.freenode.org/drupal-br - IRC Help channel (pt_BR)

3cwebdev’s picture

I wasn't aware that you could load an associative array from the DB that way. Very helpful, thanks! I have implemented the code.

The Cosmic Gift | Complete Computer Care | Team Hope