/    Sign up×
Community /Pin to ProfileBookmark

Format 6 different strings with same code

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.

[code=php]
$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>’;
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@sitehatcheryDec 05.2006 — Seems that this is what you are trying to do:
[code=php]
$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>';
}
}
[/code]
Copy linkTweet thisAlerts:
@JoshauthorDec 05.2006 — Seems that this is what you are trying to do:
[code=php]
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>';
}
}
[/code]
[/QUOTE]

Hi,

Thanks, I thought of that though.

I need to access each array element like:
[code=php]print $dex_skills[1];[/code]
Or if it's a multidimensional array:
[code=php]print $skills['dex_skills'][1];[/code]
Copy linkTweet thisAlerts:
@sitehatcheryDec 05.2006 — This will work
Copy linkTweet thisAlerts:
@sitehatcheryDec 05.2006 — 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.
Copy linkTweet thisAlerts:
@pcthugDec 05.2006 — Why not just use a function with an indefinite number of arguments:
[code=php]
$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');
}
}[/code]
Copy linkTweet thisAlerts:
@JoshauthorDec 06.2006 — I have it!
[code=php]
// 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>';
[/code]

Thanks guys for the ideas.
×

Success!

Help @Josh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...