Click to See Complete Forum and Search --> : Format 6 different strings with same code


Josh
12-05-2006, 12:10 AM
Hello,
I'm stuck! I want to format 6 different strings with the same block of code.

I'm thinking that all six strings should go into a multidimensional array. Then loop through the whole thing. But I can't seem to get it to work.

Here's what I have.

$dex_skills = "Blaster, Brawling parry, Dodge, Grenade, Vehicle blasters";
$kno_skills = "Alien species, Languages, Planetary systems, Streetwise, Value";
$mec_skills = "Astrogation, Repulsorlift operation, Space transports, Starship gunnery, Starship shields";
$per_skills = "Bargain, Con, Gambling, Hide, Search, Sneak";
$str_skills = "Brawling, Stamina, Swimming";
$tec_skills = "Computer programming/repair, First aid, Repulsorlift repair, Security, Space transports repair";

### DEX ###################################################

// FORMAT DEX SKILLS

// MAKE LOWER CASE
$dex_skills = strtolower($dex_skills);
// TRASFORM INTO AN ARRAY
$dex_skills = explode(',', $dex_skills);
// SORT
usort($dex_skills, "strnatcmp");
// TRIM AND DO OTHER STUFF
foreach ($dex_skills as &$item) {
$item = trim($item);
$item = ucwords($item);
}

### KNO ###################################################

// FORMAT KNO SKILLS
$kno_skills = strtolower($kno_skills);
$kno_skills = explode(',', $kno_skills);
usort($kno_skills, "strnatcmp");
foreach ($kno_skills as &$item) {
$item = trim($item);
$item = ucwords($item);
}

### MEC ###################################################

// FORMAT MEC SKILLS
$mec_skills = strtolower($mec_skills);
$mec_skills = explode(',', $mec_skills);
usort($mec_skills, "strnatcmp");
foreach ($mec_skills as &$item) {
$item = trim($item);
$item = ucwords($item);
}

### PER ###################################################

// FORMAT PER SKILLS
$per_skills = strtolower($per_skills);
$per_skills = explode(',', $per_skills);
usort($per_skills, "strnatcmp");
foreach ($per_skills as &$item) {
$item = trim($item);
$item = ucwords($item);
}

### STR ###################################################

// FORMAT STR SKILLS
$str_skills = strtolower($str_skills);
$str_skills = explode(',', $str_skills);
usort($str_skills, "strnatcmp");
foreach ($str_skills as &$item) {
$item = trim($item);
$item = ucwords($item);
}

### TEC ###################################################

// FORMAT STR SKILLS
$tec_skills = strtolower($tec_skills);
$tec_skills = explode(',', $tec_skills);
usort($tec_skills, "strnatcmp");
foreach ($tec_skills as &$item) {
$item = trim($item);
$item = ucwords($item);
}

// PRINT ARRAYS
print '<pre>';
print_r($dex_skills);
print_r($kno_skills);
print_r($mec_skills);
print_r($per_skills);
print_r($str_skills);
print_r($tec_skills);
print '</pre>';

sitehatchery
12-05-2006, 01:03 AM
Seems that this is what you are trying to do:

$skill=array(
'dex' =>array('Blaster', 'Brawling parry', 'Dodge', 'Grenade', 'Vehicle blasters'),
'kno' =>array('Alien species', 'Languages', 'Planetary systems', 'Streetwise', 'Value'),
'mec' => array('Astrogation', 'Repulsorlift operation', 'Space transports', 'Starship gunnery', 'Starship shields'),
'per' => array('Bargain', 'Con', 'Gambling', 'Hide', 'Search', 'Sneak'),
'str' => array('Brawling', 'Stamina', 'Swimming'),
'tec' => array('Computer programming/repair', 'First aid', 'Repulsorlift repair', 'Security', 'Space transports repair')
);

foreach($skill as $key=>$val){
echo '<h3>'.strtolower($key).' skills</h3>';
usort($skill[$key], "strnatcmp");
foreach($skill[$key] as $key2=>$val2){
echo '<p>'.ucwords($val2).'</p>';
}
}

Josh
12-05-2006, 01:39 AM
Seems that this is what you are trying to do:

foreach($skill as $key=>$val){
echo '<h3>'.strtolower($key).' skills</h3>';
usort($skill[$key], "strnatcmp");
foreach($skill[$key] as $key2=>$val2){
echo '<p>'.ucwords($val2).'</p>';
}
}

Hi,
Thanks, I thought of that though.

I need to access each array element like:
print $dex_skills[1];
Or if it's a multidimensional array:
print $skills['dex_skills'][1];

sitehatchery
12-05-2006, 10:09 AM
This will work

sitehatchery
12-05-2006, 10:12 AM
If you want to keep the casing, you may want to overwrite the array values in the foreach loop so that when you print it out, you get the result you need without further processing.

pcthug
12-05-2006, 03:51 PM
Why not just use a function with an indefinite number of arguments:

$dex_skills = "Blaster, Brawling parry, Dodge, Grenade, Vehicle blasters";
$kno_skills = "Alien species, Languages, Planetary systems, Streetwise, Value";
$mec_skills = "Astrogation, Repulsorlift operation, Space transports, Starship gunnery, Starship shields";
$per_skills = "Bargain, Con, Gambling, Hide, Search, Sneak";
$str_skills = "Brawling, Stamina, Swimming";
$tec_skills = "Computer programming/repair, First aid, Repulsorlift repair, Security, Space transports repair";

formatSkills($dex_skills, $kno_skills, $mec_skills, $per_skills, $str_skills, $tec_skills);

print '<pre>';
print_r($dex_skills);
print_r($kno_skills);
print_r($mec_skills);
print_r($per_skills);
print_r($str_skills);
print_r($tec_skills);
print '</pre>';

function formatSkills()
{
if ( func_num_args() < 1 )
{
return trigger_error('No arguments passed.');
}

foreach ( func_get_args() as $key )
{
global $$key;
$$key = explode(', ', strtolower($$key));
usort($$key, 'strnatcmp');
}
}

Josh
12-05-2006, 10:10 PM
I have it!

// SKILL STRINGS
$dex_skills = "Blaster, Brawling parry, Dodge, Grenade, Vehicle blasters";
$kno_skills = "Alien species, Languages, Planetary systems, Streetwise, Value";
$mec_skills = "Astrogation, Repulsorlift operation, Space transports, Starship gunnery, Starship shields";
$per_skills = "Bargain, Con, Gambling, Hide, Search, Sneak";
$str_skills = "Brawling, Stamina, Swimming";
$tec_skills = "Computer programming/repair, First aid, Repulsorlift repair, Security, Space transports repair";

// CREATE AN ARRAY FROM SKILL STRINGS
$skills = array (
'dex_skills' => $dex_skills,
'kno_skills' => $kno_skills,
'mec_skills' => $mec_skills,
'per_skills' => $per_skills,
'str_skills' => $str_skills,
'tec_skills' => $tec_skills
);

// TURN SKILL STRINGS INTO ARRAYS
foreach ($skills as &$key) {
$key = explode(',', $key);
foreach ($key as &$value) {
$value = strtolower($value);
$value = trim($value);
$value = ucwords($value);
}
usort($key, "strnatcmp");
}

// PRINT SKILL ARRAYS
print '<pre>';
print_r($skills['dex_skills']);
print_r($skills['kno_skills']);
print_r($skills['mec_skills']);
print_r($skills['per_skills']);
print_r($skills['str_skills']);
print_r($skills['tec_skills']);
print '</pre>';

Thanks guys for the ideas.