Modifying PHP function to accept different types of arrays -
i have small function below accepts associative arrays , i'm struggling find way modify accept simpler arrays. easy create separate function , remove foreach loop, i'm trying see if there more efficient way achieve this. insights appreciated.
function:
public function set_content($file, $data, $section_name) { $jobs = new template_engine(); $jobs->set_file($file); $jobs_output = ''; static $section_title = 0; foreach($data $job) { //print_r($job); foreach($job $key=>$value) { $jobs->set($key,$value); } if ($section_title === 0) { $jobs->set("section_title",$section_name); } else { $jobs->clear_set("section_title"); } ++$section_title; $jobs_output .= $jobs->output(); } $section_title = 0; return $jobs_output; }
array sample 1:
array ( [0] => array ( [custom_id] => 78 [name] => title goes [description] => test [resume_id] => 96 [order_id] => 0 [section_id] => 224 [profile_id] => 38 [user_id] => 1 [vanity_name] => sample of template 3 [template_id] => 3 [date_add] => 0000-00-00 00:00:00 [date_mod] => 2012-03-04 11:00:05 ) [1] => array ( [custom_id] => 76 [name] => custom item [description] => descrition custom item goes here. [resume_id] => 96 [order_id] => 1 [section_id] => 224 [profile_id] => 38 [user_id] => 1 [vanity_name] => sample of template 3 [template_id] => 3 [date_add] => 0000-00-00 00:00:00 [date_mod] => 2012-03-04 11:00:05 ) )
array sample 2:
array ( [list_item] => englishspanishfrenchcatalanjapanese )
add if before foreach($job $key=>$value)
check if first item array or not:
... if (is_array($job)) { foreach($job $key=>$value) { $jobs->set($key,$value); } } else { // treat here value of more simpler array; // in case, $job contain englishspanishfrenchcatalanjapanese } ...
Comments
Post a Comment