ashita.org
HTTP/1.1 503 Service Unavailable 访问时间:2015年10月02日 11:08:03 类型:text/html; charset=UTF-8 Transfer-Encoding: chunked 连接:keep-alive 设置Cookie:__cfduid=d67be0cbbc1caeb44e4e1fbaa0ba518af1443755282; expires=Sat, 01-Oct-16 03:08:02 GMT; path=/; domain=.ashita.org; HttpOnly 语言环境:PHP/5.4.16 X-Pingback: http://www.ashita.org/xmlrpc.php 过期时间:1984年01月11日 13:00:00 缓存控制:不缓存,必须更新, max-age=0 其他指令:不缓存 Retry-After: 3600 服务器:cloudflare-nginx CF-RAY: 22ed335591b111b3-SJC 网站编码:UTF-8
Skip to content
Skip to menu
Ashita.org
Search
HomeProjects
Mar
11
2011
Multi-dimensional and associative POST arrays
Category: PHP — Jon @ 5:59 am
Anyone doing web development requiring client-server interaction is familiar with GET and POST. (There are others as well, including PUT, DELETE, and so on. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html for the list.) And pretty quickly most folks become aware of the ability to pass arrays as request parameters. For example:
<form method="post" action="http://www.example.com/myformproc.php">
<fieldset>
<legend>Notification Methods<legend>
<input type="checkbox" name="note_opts[]" value="email" id="note_opt_email"><label for="note_opt_email">E-Mail</label>
<input type="checkbox" name="note_opts[]" value="text" id="note_opt_text"><label for="note_opt_text">Text Message</label>
... etc ...
</fieldset>
</form>
On the server side, if the user selected any of the notification options, you would have an array in a POST variable, ‘note_opts’. In PHP, for example you would see an array $_POST['note_opts']. That array would contain the values of the selected options. The indexes of those values would be numerically indexed in the usual, default, manner.
What if we’re editing the options for multiple objects. Each of our objects has an id number and 0 or more of the options.
What I often see is:
<input type="checkbox" name="obj_<?php echo $object->getID(); ?>_note_opts[]" value="email" id="note_opt_email">
And then we end up having to parse the id out of the variable name…
Far better to do something like:
<input type="checkbox" name="note_opts[<?php echo $object->getID(); ?>][]" value="email" id="note_opt_email">
In this way we get a 2 dimensional array of note_opts. The object with id ’1′ is simply $_POST['note_opts'][1].
This also makes it easy to loop through the objects getting their ids along the way
$note_opts = $_POST['note_opts'];
if (is_array($note_opts)) {
foreach($note_opts as $obj_id=>$opt_set) {
$obj = MyType::get($obj_id);
if (!$obj) {
addError("Failed to get object (".$obj_id.")";);
} elseif (!$obj->setOptions($opt_set)) {
addError("Failed to set options for ".$obj->getName()." (".$obj_id.")";);
}
}
}
It’s not a big leap from note_opts[] to note_opts[2][email_options][address]. Organizationally it makes life a lot easier, especially if there is any complexity to your data. This way hierarchical data can easily be represented in nested arrays. One final comment, please note that the named array indices like email_options and address are not enclosed in quotes.
Comments (0)
Mar
11
2011
var self = this; huh?
Category: JavaScript — Jon @ 4:40 am
This is a subject that comes up frequently on StackOverflow. Some questions approach it from seeing it in code but not understanding why, while others approach it from the opposite si
© 2010 - 2020 网站综合信息查询 同IP网站查询 相关类似网站查询 网站备案查询网站地图 最新查询 最近更新 优秀网站 热门网站 全部网站 同IP查询 备案查询
2025-12-12 15:28, Process in 0.0062 second.