取消选择数组中的某些键

时间:2014-04-14 08:22:46

标签: php arrays

我使用数组填充表单。现在一切正常,现在想要取消某些字段的数组键。然后再次在该字段之后我想要键值。

我可以使用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;
    }

}

3 个答案:

答案 0 :(得分:3)

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

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

然后使用(循环中)

echo form_input(array_intersect_key($value, $white_list));

答案 1 :(得分:1)

$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;
}

}

答案 2 :(得分:1)

更高级的解决方案是创建自己的数组类并添加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
)