取消选择数组中的某些键


Deselect certain keys in array

我正在使用数组填充表单。现在一切都很好,只是想为一些字段取消设置一些数组键。然后在那个字段之后,我再次想要键值。

我可以使用unset,但它会删除密钥以供进一步使用。

这是我所拥有的和我想要得到的。

$fields = array(
        'type' => 'text',
        'class' => 'col-6',
        'name' => 'my-name',
        'container' => 'col-12',
        'id' => 'my-id',
    );
foreach($fields as $value)
{
    switch ($value['type'])
    {
        case 'text':
        echo array_key_exists('field_class', $value) ? '<div class="' . $value['container'] . '">' : NULL;
        // here I want to unset 'container' and 'id' keys
        echo form_input($value);
        // here I want to get 'container' and 'id' value back (I need 'id' badk for some reason)
        echo array_key_exists('container', $value) ? '</div>' : NULL;
        default:
            break;
    }
}

在循环之前,您可能需要定义:

$white_list = array_flip(array('type', 'class', 'name'));

然后使用(环路中)

echo form_input(array_intersect_key($value, $white_list));
$fields = array(
    'type' => 'text',
    'class' => 'col-6',
    'name' => 'my-name',
    'container' => 'col-12',
    'id' => 'my-id',
);
foreach($fields as $value)
{
switch ($value['type'])
{
    case 'text':
    echo array_key_exists('field_class', $value) ? '<div class="' . $value['container'] . '">' : NULL;

    $temp = $value;
    unset($temp['container']);
    unset($temp['id']);
    // here I want to unset 'container' and 'id' keys
    echo form_input($temp);
    // here I want to get 'container' and 'id' value back (I need 'id' badk for some reason)
    echo array_key_exists('container', $value) ? '</div>' : NULL;
    default:
        break;
}

}

更高级的解决方案是创建自己的数组类并添加saverevert方法:

<?php
class ArraySaver extends 'ArrayIterator {
    private $savedStates = array();
    private $stateIDCounter;
    private $stateIDLast;
    public function __construct(array $array) {
        foreach ($array as $key => $value) {
            $this[$key] = $value;
        }
    }
    public function save($stateID = null) {     
        if (empty($stateID)) {
            $this->stateIDCounter++;
            $stateID = $this->stateIDCounter;
        }
        $this->savedStates[$stateID] = array();
        foreach ($this as $key => $value) {
            $this->savedStates[$stateID][$key] = $value;
        }
        $this->stateIDLast = $stateID;
        return $stateID;
    }
    public function revert($stateID = null) {
        if (empty($stateID)) {
            $stateID = $this->stateIDLast;
        }
        // Remove keys that weren't existent in our save
        foreach ($this as $key => $value) {
            if (!isset($this->savedStates[$stateID][$key])) {
                unset($this[$key]);
            }
        }
        // Add values
        foreach ($this->savedStates[$stateID] as $key => $value) {
            $this[$key] = $value;
        }
    }
}

用法:

<?php
// Note that we use "new ArraySaver"
$fields = new ArraySaver(array(
    'type' => 'text',
    'class' => 'col-6',
    'name' => 'my-name',
    'container' => 'col-12',
    'id' => 'my-id',
));
$originalSave = $fields->save(); //Save the state ID in "$originalSave"
$fields['bar'] = 'Yes - at a bar!';
$fields->save('barbar'); //Call this state "barbar"
print_r((array)$fields); //Output #1
unset($fields['bar'], $fields['container'], $fields['id']);
$fields['extra'] = 123;
print_r((array)$fields); //Output #2
$fields->revert($originalSave); //Use the state ID in "$originalSave" to revert
print_r((array)$fields); //Output #3

输出

输出#1

Array (
    [type] => text
    [class] => col-6
    [name] => my-name
    [container] => col-12
    [id] => my-id
    [bar] => Yes - at a bar!
)

输出#2

Array (
    [type] => text
    [class] => col-6
    [name] => my-name
    [extra] => 123
)

输出#3

Array (
    [type] => text
    [class] => col-6
    [name] => my-name
    [container] => col-12
    [id] => my-id
)