Re: Comparing string to array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Davey wrote:
Hi all,

Ok it's 2am, my brain has gone to mush and I am having trouble
figuring out an easy way to do this, can anyone shed some light?

Take a peek at the following code:

// START
<pre>
<?php
    print_r($_POST);
$userparam = "test['sam'][]";

    //  How to check if $userparam exists in the $_POST array
    //  and get all the values from it?
// Obviously this won't work, but you get the idea:
    if (isset($_POST[$userparam]))
    {
        echo 'yeah';
        $values = $_POST[$userparam];
    }
    else
    {
        echo 'nah';
    }
?>
</pre>

<form method="post">

<input type="checkbox" name="test['bob'][]" value="red">red<br>
<input type="checkbox" name="test['bob'][]" value="green">green<br>
<input type="checkbox" name="test['bob'][]" value="blue">blue<br>
<input type="checkbox" name="test['sam'][]" value="red2">red2<br>
<input type="checkbox" name="test['sam'][]" value="green2">green2<br>
<input type="checkbox" name="test['sam'][]" value="blue2">blue2<br>

<input type="submit">

</form>
// END

From the code above I'm trying to figure out how to tell if the
$userparam exists in the $_POST array. PHP automatically expands the
form element name into multi-dim arrays within $_POST, so a simple
'isset' as shown in the code above won't play because it's got a
totally useless array key passed to it.

I need a way to turn the string:

"test['sam'][]"

into something I can look into $_POST for.

Any ideas? The coffee boost is wearing off, but I want to get this
licked tonight :-\

Cheers,

Rich
taking a step back and looking a little closer at the problem.  I have come up with this

Which does not require eval()


Maybe this will work for the OP

<pre>
<?php

$param = 'test[sam][colors][]';

function find_within_array($string, $source) {
	$data = array();
	$tmp = $source;
	if ( preg_match_all('/(\[?([\w]+)\]?)/', $string, $matches, PREG_SET_ORDER) ) {
		if ( count($matches) > 1 ) {
			for ( $i = 0; $i<count($matches); $i++ ) {
				if ( isset($tmp[$matches[$i][2]]) ) {
					if ( is_array($tmp[$matches[$i][2]]) ) {
						foreach ($tmp[$matches[$i][2]] AS $value ) {
							if ( is_string($value) ) {
								$data[] = $value;
							}
						}
					}
					$tmp = $tmp[$matches[$i][2]];
				}
			}
			return $data;
		}
	}
	return array();
}

$results = find_within_array($param, $_POST);
if ( count($results) > 0 ) {
	echo "yeah\n";
	echo join(':', $results);
} else {
	echo 'nah';
}

?>
</pre>

<form method="post">

<input type="checkbox" name="test[bob][colors][]" value="red">red<br>
<input type="checkbox" name="test[bob][colors][]" value="green">green<br>
<input type="checkbox" name="test[bob][colors][]" value="blue">blue<br>
<input type="checkbox" name="test[sam][colors][]" value="red">red<br>
<input type="checkbox" name="test[sam][colors][]" value="green">green<br>
<input type="checkbox" name="test[sam][colors][]" value="blue">blue<br>

<input type="submit">

</form>


--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux