470,848 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Calling array keys with embedded variables

Hello,

I'm in the middle of modifying a script and I've come across a problem.
I have a form that gets repeated x number of times and each field in
the form gets the name $item_number_ . $business_item_index ._(the
specific field name). These varaibles get reposted to the same page as
$_POST variables, so that the forms save the users settings. What I'm
trying to do is parse through those fields that the user did not set,
and if they are null, to unset them and decrement the counter
($business_item_index) so that one less form gets written to the screen
the next time the user submits.

My problem is that when I parse through them like this:

if ($_POST['item_number_'.$business_item_index.'_title'] == NULL){
unset ($_POST['item_number_'.$business_item_index.'_title']);
$business_item_index --;
}

Php sees that variable key I'm calling as
item_number_ 1_title
even though I defined $business_item_index as a variable and it works
just fine if I decrement or increment it.

Note the white space after "number_". I can't figure out why php is
prepending a space to my integer.

My actual code is alot more complicated than this, because I'm doing a
foreach statement and splitting the $_POST global array into key =
value and then unsetting all the keys that have
item_number_$business_item_index as a substr($key, 0,13), so that all
of the fields for that business item form gets unset. But I think the
above example is sufficient to explain the problem. If you need to see
a more detailed code block I can post it.

Thanks for any assistance.

Oct 19 '05 #1
1 1301
well, not going deep to the cause you could use
trim($business_item_index)
to get rid of first and last spaces if $business_item_index is a string
after all or you could try to convert string to int like this
intval($business_item_index) it should also remove your leading space

Oct 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andy Levy | last post: by
5 posts views Thread by Phil Powell | last post: by
30 posts views Thread by josh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.