By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,746 Members | 1,780 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,746 IT Pros & Developers. It's quick & easy.

function works, array_walk fails

P: n/a
<?php

class Grad {

var $dbFormExemptionArray = array();

function Grad ($id = '') {

/*----------------------------------------------------------------------------------------------------------------------
Do note that if you are generating arrays that will not have
their values dynamically placed into the
db insert or update statements you *MUST* place the name
'array' or 'Array' (or any variant capitalization
you like) into the name of the object array else it will be
assumed to be part of the get_class_vars() array
call dynamically parsed to produce the SQL strings
-----------------------------------------------------------------------------------------------------------------------*/

// USED FOR SPECIAL CASES WHEN GENERATING SQL STATEMENTS TO
COMPARE FORM NAMES TO DB NAMES OR VICE VERSA
$this->dbFormExemptionArray =
array('instant_messenger_service_id' => 'instantMessenger',
'is_citizen' => 'citizen',
'salary_range' => 'salary',
'grad_preferred_org_description' =>
'gradPreferredOrgDesc',
'federal_agencies' => 'agencies',
'has_security_clearance' => 'hasClearance',
'security_clearance_level_id' => 'clearanceID',
'grad_ideal_job' => 'idealJob',
'language_id' => 'languages',
'language_rank_id' => 'proficiency',
'salary' => 'jobSalary',
'start_date' => 'jobStartDate',
'end_date' => 'jobEndDate',
'hours_per_week' => 'jobHours',
'job_duties_accomplishments' => 'jobDuties',
'languages_other' => 'language_other'
);

}
function arrayStrToUpper(&amp;$element) { // PRIVATE STRING
"METHOD" FOR array_walk
$allCapsArray = array('id', 'gpa');
if (in_array($element, $allCapsArray)) return
strtoupper($element);
return ucfirst($element);
}

function dbNameToFormName($formField) { // STRING "METHOD"
$formDBExemptionArray = array_flip($this->dbFormExemptionArray);
if (array_search($formField, $formDBExemptionArray) !== false
&amp;&amp; array_search($formField, $formDBExemptionArray) !== null) {
return array_search($formField, $formDBExemptionArray);
} else {
$dbNameArray = explode('_', $formField);
$classMethodCompartmentArray = array();
$classMethodCompartmentArray[0] = $this;
$classMethodCompartmentArray[1] = 'arrayStrToUpper';
array_walk($dbNameArray, $classMethodCompartmentArray);
$dbNameArray[0] = strtolower($dbNameArray[0]);
return implode('', $dbNameArray);
}
}

}

$grad =&amp; new Grad;

foreach(array('ethnicity_id', 'ethnicity_other', 'start_date',
'fname', 'career_level_id') as $key => $val) {
print_r("val = $val and converted it is = " .
$grad->dbNameToFormName($val) . "<P>");
}

$grad = null;

?>
This block of code fails to do what it is supposed to do: take a
patterned string and convert it to being a mixed-case variable.

Here is the actual output if you ran it now:
val = ethnicity_id and converted it is = ethnicityid

val = ethnicity_other and converted it is = ethnicityother

val = start_date and converted it is = jobStartDate

val = fname and converted it is = fname

val = career_level_id and converted it is = careerlevelid
Here is the way the output is SUPPOSED to look like instead:
val = ethnicity_id and converted it is = ethnicityID

val = ethnicity_other and converted it is = ethnicityOther

val = start_date and converted it is = jobStartDate

val = fname and converted it is = fname

val = career_level_id and converted it is = careerLevelID
the function itself works perfectly, however, as a callback function
it seems to do nothing in array_walk, furthermore, array_walk returns
a 1 meaning the results are valid. Nonetheless, the array elements
remain unchanged, nor is any scalar variable placed into the original
class method that has array_walk.

This is the second time array_walk has failed to do for me what it is
advertised to do (see
http://us2.php.net/manual/en/function.array-walk.php). Is there an
alternative to array_walk in PHP or do I have to search outside of PHP
to do such a simple yet infuriating task?

Phil
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.