我一直在使用array_slice但它无法正常工作。一点都不。
所以我有36个复选框..我先做这个。
$checks = $_POST['ck'];
然后我这样做。
$newAr = array_slice($checks, 0, 8);
echo implode(',', $newAr);
关键是,我想$ newAr只显示在数组中的方框0 - 8中选中的方框。
接下来就是这样。
$newAr2 = array_slice($checks, 9, 20);
echo implode(',', $newAr2);
此处显示方框9-20中的方框,依此类推......
以下是如何为这些框子完成HTML ...
<input type="checkbox" value="Demo" name="ck[]" id="ck1">
当我这样做时...当我回显$ newAr或$ newAr2时..它只显示每个复选框的值1 - 36
答案 0 :(得分:4)
为了使这项工作,您需要稍微破解HTML:
<input type="hidden" name="ck[0]" value="">
<input type="checkbox" value="Demo0" name="ck[0]" id="ck1">
<input type="hidden" name="ck[1]" value="">
<input type="checkbox" value="Demo1" name="ck[1]" id="ck1">
<input type="hidden" name="ck[2]" value="">
<input type="checkbox" value="Demo2" name="ck[2]" id="ck1">
<!-- repeat until name="ck[35]" -->
说明:
<input type="hidden" name="ck[0]" value="">
将确保$_POST['ck'][0]
始终设置且为空字符串。
<input type="checkbox" value="Demo0" name="ck[0]" id="ck1">
确保如果选中该复选框,则$_POST['ck'][0]
将为“Demo0”
答案 1 :(得分:1)
根据评论,这不是array_slice
的工作原理。然而,敲一个能满足你要求的功能是非常微不足道的。
正如MonkeyZeus指出的那样,您还需要更改HTML以在后请求中接收正确的索引。
function array_slice_by_key($array, $min, $max)
{
return array_intersect_key($array, array_combine(range($min, $max), range($min, $max)));
}
// For example
$_POST["ck"] = array(1 => 1, 4 => 1, 9 => 1, 10 => 1, 15 => 1, 22 => 1, 26 => 1);
$checks = isset($_POST["ck"]) ? $_POST["ck"] : array();
var_dump(array_slice_by_key($checks, 1, 8));
/*
array (size=2)
1 => int 1
4 => int 1
*/
var_dump(array_slice_by_key($checks, 9, 20));
/*
array (size=3)
9 => int 1
10 => int 1
15 => int 1
*/
var_dump(array_slice_by_key($checks, 21, 36));
/*
array (size=2)
22 => int 1
26 => int 1
*/
答案 2 :(得分:1)
Website demonstration: Checkboxes - current and previous state
问题在于复选框是当用户取消选中时没有输入。此类可用于记录正在显示的复选框以及是否取消选中它们。
当您想知道复选框状态是否刚刚更改时,它非常有用。
它还会生成值以分配给html中的复选框。
工作原理:
根据键(cbxDetails)为每个复选框创建了各种详细信息:
html中使用的值 - (cbxValue)_
详情的关键 - _(关键)
复选框状态是否已更改 - (hasChanged)
这些细节也可以通过数组迭代器 - (getIterator)
获得。处理:
构建表单:
使用
生成复选框html中使用的值 - (cbxValue)_
将所有复选框的当前状态存储在隐藏字段中,例如&#39; cbxAllStates&#39; - (cbxStateString)
输入表单时,假设使用$ _POST:
全部完成
// look after the checkbox states
$cbxHistory = new CheckboxHistory($vars);
if (isset($_POST['allBoxes'])) {
if (isset($_POST['allBoxes']['cbxChecked'])) {
$cbxHistory->fromCheckedList($_POST['allBoxes']['cbxChecked']);
}
$cbxHistory->fromCbxStateString($_POST['allBoxes']['history']);
}
// extract arrays for testing
$extract1 = array();
$extract2 = array();
$extract3 = array();
$cbxIter = $cbxHistory->getIterator();
for ($i = 0; $i <= 8; $i++) { // extract first 9 entries
$extract1[] = $cbxIter->current();
$cbxIter->next();
}
for ($i = 0; $i <= 12; $i++) { // extract next 12 entries
$extract2[] = $cbxIter->current();
$cbxIter->next();
}
while ($cbxIter->valid()) { // extract remaining entries
$extract3[] = $cbxIter->current();
$cbxIter->next();
}
<?php // http://stackoverflow.com/questions/36849297/variable-to-specific-parts-of-array
/**
* Maintain a list of checkboxes and the immidiate history
*
* Uses: array of $key => $label
*
* the key can be numbers or string the class works with keys
*
* e.g. $cbxHistory = new CheckboxHistory(array('key1' => 'label 1',
* 'key2' => 'label 2',
* 'key3' => 'label 3',
* ))
*
* It uses a string to hold whether each checkbox in the list currently is checked or not
*
* e.g. $cbxHistory->cbxStateString()
*
* This will be stored in a hidden field $_POST and will become the $prvCheckedState
*
*
*
* It generates all the details to
* 1) use in the checkbox HTML
* 'label' is for the html checkbox label
* 'value' is for the html checkbox value
* note: the value is a position in the list not a key!
*
* 2) The state of the checkbox and whether is has just changed
*
* e.g. $cbxHistory->cbxDetails($key);
*
* returns: array('label' => $this->cbxLabel($key),
* 'value' => $this->cbxValue($key),
* 'key' => $key,
*
* 'hasChanged' => $this->hasChanged($key),
* 'curChecked' => $this->curChecked($key),
* 'prvChecked' => $this->prvChecked($key));
*
* It uses a cbxStateString to know what the previous states of the checkbox
*
* e.g. $cbxHistory->fronCbxCheckedState($oldCbxCheckedState);
*
* This will normally be from the html 'hidden' cbxState field
*
*/
class CheckboxHistory {
protected $valueList = null;
protected $curCheckedState = '';
protected $prvCheckedState = '';
protected $keyList = null; // needed to find the position of the key
public function __construct(array $valueList, $prvStateStr = '')
{
$this->valueList = $valueList;
$this->curCheckedState = str_repeat('0', count($this->valueList));
$this->prvCheckedState = str_repeat('0', count($this->valueList));
$this->keyList = array_keys($valueList);
if (!empty($prvStateStr)) {
$this->fromCbxStateString($prvStateStr);
}
}
/**
* The label value to be used in the html
*
* @param mixed $key
*
* @return label text
*/
public function cbxLabel($key)
{
return $this->valueList[$key];
}
/**
* The value to be used for the checkbox in the html.
*
* It is actually the postion of the lookup key in the list
*
* @param mixed $key
*
* @return integer
*/
public function cbxValue($key)
{
return $this->keyToPosition($key);
}
/**
* This is the current state vector of all the checkboxes
*
* It is stored in a 'hidden' field on the form
*
* It is used in the fromCbxStateString() method
*
* @return string
*/
public function cbxStateString()
{
return $this->curCheckedState;
}
/**
* All the details (checkbox state) in one convenient list
*
* @param mixed $key
*
* @return array
*/
public function cbxDetails($key)
{
return array('label' => $this->cbxLabel($key),
'value' => $this->cbxValue($key),
'key' => $key,
'hasChanged' => $this->hasChanged($key),
'curChecked' => $this->curChecked($key),
'prvChecked' => $this->prvChecked($key),
);
}
/**
* All the cbxDetails as an iterator
*
* @return \ArrayIterator
*/
public function getIterator()
{
$details = array();
foreach($this->keyList as $key) {
$details[] = $this->cbxDetails($key);
}
return new \ArrayIterator($details);
}
/**
* Set or unset a checkbox by key value
*
* @param mixed $key
* @param boolean $checked
*
* @return void
*/
public function setCheckbox($key, $checked = true)
{
$keyPos = $this->keyToPosition($key);
$this->curCheckedState[$keyPos] = $checked ? '1' : '0';
}
/**
* current state of a checkbox
*
* @param mixed $key
*
* @return boolean
*/
public function curChecked($key)
{
$keyPos = $this->keyToPosition($key);
return (bool) $this->curCheckedState[$keyPos];
}
/**
* previous state of a checkbox
*
* @param mixed $key
*
* @return booleam
*/
public function prvChecked($key)
{
$keyPos = $this->keyToPosition($key);
return (bool) $this->prvCheckedState[$keyPos];
}
/**
* Has the checkbox changed state (user checked or unchecked it)
*
* @param mixed $key
*
* @return boolean
*/
public function hasChanged($key)
{
$keyPos = $this->keyToPosition($key);
return $this->curCheckedState[$keyPos] !== $this->prvCheckedState[$keyPos];
}
/**
* set the curCheckedState from an array of values ($positions)
*
* @param array $positionList i.e. $_POST
*
* @return void
*/
public function fromCheckedList(array $checkedPos)
{
$this->curCheckedState = str_repeat('0', count($this->valueList));
foreach ($checkedPos as $position) {
$this->setCheckbox($this->keyList[$position], true);
}
}
/**
* This is the previous state of the all checkboxes
*
* It is obtained from the chechboxes 'hidden' field
*
* @param string $prvStateStr
*
* @return void
*/
public function fromCbxStateString($prvStateStr)
{
// must be the correct lentgth
$this->prvCheckedState = str_pad($prvStateStr, count($this->valueList), $this->prvCheckedState);
}
// given a key get the postion of the key in the list
protected function keyToPosition($key)
{
return array_search($key, $this->keyList);
}
}