Singleton类,多维数组unset元素

时间:2014-10-07 08:51:40

标签: php multidimensional-array unset

我正在尝试从多维数组中删除不相关的数据。

阵列看起来像这样:

[6] => Array
    (
        [710C27E0-822A-4513-9D44-D97E929484A9] => Array
            (
                [documents] => Array
                    (
                        [0] => error message
                    )

            )

    )

我正在使用以下代码:

<?php
class report {
private static $instance;
private $data = array();

private function __construct() { }

private function __clone() { }

public function __destruct() { }

public static function singleton() {
    if (!isset(self::$instance))
        self::$instance = new self();

    return self::$instance;
}

public function check() {
    foreach($this->data as $key => &$values) {
        foreach($values as $k => &$value) {
            /* some checks */

            if($return != null)
                $values[$return] = $values[$k];

            unset($values[$k]);
        }
        if(sizeof($values) == 0)
            unset($this->data[$key];
    }
}

public function __toString() {
    print_r($this->data);
    return '';
}
}

当我用以下代码替换上部检查功能时,它正在工作,但我正在寻找一些更好/更清洁,特别是更快的解决方案。

public function check() {
    $_data = json_encode($this->data);
    $data = json_decode($_data);

    foreach($data as $key => &$values) {
        foreach($values as $k => &$value) {
            /* some checks */
            if($return != null)
                $values->$return = $values->$k;

            unset($values->$k);
        }
    }
    $this->data = $data;
}

已解决但不理解..

public function log($id, $guid, $job, $message) {
    $this->data[$id][$guid][$job][] = $message;
}

当我var_dump($ guid)它返回:

string(36) "710C27E0-822A-4513-9D44-D97E929484A9"

现在我做了一个字符串演员:

var_dump((string)$guid);

返回:

string(36) "710C27E0-822A-4513-9D44-D97E929484A9"

$this->data[$id][(string)$guid][$job][] = $message;

作品!

0 个答案:

没有答案