Jump to content

Help needed with an associative array


fiasst

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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)) {
    // ...
}
...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.