fiasst Posted June 14, 2010 Share Posted June 14, 2010 Hi there, I'm having real trouble writing a script to print hidden form fields to my page from an array. I've been trying to print <input type="hidden" name="[array_key]" value="[array_value]" /> for each item that has 'build_' included in the key. I've ran out of time and talent and would really appreciate some help! Here's an example of my array and below the outcome i'd like returned to the page: Array( [build_AGE] => 20 [foo] => dont use this as it doesn't have 'build_' in the key [bUILD_OPTIONS] => Array( [build_HOUR] => 06 [build_MIN] => 10 [build_DATE] => Array( [month] => 6 [day] => 14 [year] => 2010 ) ) ) <input type="hidden" name="build_AGE" value="20" /> <input type="hidden" name="build_OPTIONS" value="Array" />//I don't actually need this but it seems too complicated to avoid, no harm in leaving it <input type="hidden" name="build_DATE" value="6,14,2010" /> <input type="hidden" name="build_HOUR" value="06" /> <input type="hidden" name="build_MIN" value="10" /> Here's my attempt. As you can probably tell I was getting carried away and, I think, over complicating it: if (is_array($form_values)){ foreach ($form_values as $k => $v){ if (!empty($v) && preg_match('/^build/', $k)){ if (is_array($v)){ foreach ($v as $k2 => $v2){ if (is_array($v2)){ $v2 = implode(",", array_values($v2)); print '<input type="hidden" name="'.$k.'" value="'.$v2.'" />'; } else { print '<input type="hidden" name="'.$k.'" value="'.$v2.'" />'; } } } else { print '<input type="hidden" name="'.$k.'" value="'.$v.'" />'; } } } } Thanks, Marc Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/ Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 You should use a recursive function. Try something like this: function hidden_fields_recursive($arr) { foreach($arr as $key => $val) { if(is_array($val) && strtolower($key) == 'build_date') { echo "<input type='hidden' name='$key' value='" . implode(',', $val) . "' />"; } else if(is_array($val)) { hidden_fields_recursive($val); } else { if(strtolower(substr($key, 0, 6)) == 'build_') { echo "<input type='hidden' name='$key' value='$val' />"; } } } } $arr = array( 'build_AGE' => 20, 'foo' => 15, 'BUILD_OPTIONS' => array( 'build_HOUR' => 06, 'build_MIN' => 10, 'build_DATE' => array( 'month' => 6, 'day' => 14, 'year' => 2010 ) ) ); hidden_fields_recursive($arr); Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072001 Share on other sites More sharing options...
fiasst Posted June 14, 2010 Author Share Posted June 14, 2010 Hi Alex, thanks for replying. This code is spot on appart from one array instance. I've stripped the code down temporarily for ease: function hidden_fields_recursive($arr){ foreach($arr as $key => $val) { if (is_array($val)){ hidden_fields_recursive($val); } else { if (substr($key, 0, 6) == 'build_') { echo 'name: '.$key.' | value: '.$val; } } } } $arr = array( "build_age" => 20, "build_list" => array( 11 => 11, 7 => 7, 8 => 8, ), "build_options" => array( "build_date" => array( "month" => 6, "day" => 14, "year" => 2010, ), "build_hour" => 10, "build_hour" => 17, ), ); hidden_fields_recursive($arr); Results in: name: build_age | value: 20 name: 11 | value: 11 name: 7 | value: 7 name: 8 | value: 8 name: build_options | value: Array name: build_date | value: Array//(can't always guarantee the name will be build_date) name: month | value: 6 name: day | value: 14 name: year | value: 2010 ...ect but I'm hoping to return the build_list and build_date values as strings. ie: name: build_list | value: 11,7,8 name: build_date | value: 6,14,2010 Hope that makes sense, thanks again for any help Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072054 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 How are you supposed to know when an array should be imploded to form the value, or if it contains values that should be separate hidden inputs? You have to have some indication, there isn't a function to read minds. Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072059 Share on other sites More sharing options...
fiasst Posted June 14, 2010 Author Share Posted June 14, 2010 I've been trying to find a way of checking if build_date is an Array and if so, do any of the array_values contain another array. If it doesn't then implode the values. "build_date" => array( "month" => 6, "day" => 14, "year" => 2010, ), I just can't for the life of me code it, hehe. Been trying since sunday morning! Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072088 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 Make a simple function to check if any of the array values contain an array, like so: function contains_array($arr) { foreach($arr as $item) { if(is_array($item)) { return true; } } return false; } Then inside the loop in the other function I provided: ... if(strtolower($key) == 'build_date' && is_array($val) && !contains_array($val)) { // ... } ... Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072089 Share on other sites More sharing options...
fiasst Posted June 14, 2010 Author Share Posted June 14, 2010 That's the ticket! *massive sigh of relief* Thank you so much for your help! I kept re-writing it and always ended up with about 5 foreach() statements before getting confused. Really appreciate it :] Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072099 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 You're welcome. By the way, there's a way to mark a topic solved at the bottom left of the page. Quote Link to comment https://forums.phpfreaks.com/topic/204759-help-needed-with-an-associative-array/#findComment-1072103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.