从字符串问题创建数组

时间:2014-12-16 22:34:13

标签: php

我有一个从字符串创建多维数组的函数。以下是每个字符串的输出结果:

字符串:

app.name.version

app.vendor
  

注意:这些是从数据库中检索的字符串

输出:

['app']['name']['version']

['app']['vendor']

我相应地分配它们的值。当我在表示子数组的索引号的字符串中包含数字时出现问题。这是一个例子:

shifts.breaks.unpaid.0.description
shifts.breaks.unpaid.0.duration
shifts.breaks.unpaid.1.description
shifts.breaks.unpaid.1.duration

带输出:

Array
(
    [unpaid] => Array
        (
            [0] => Array
                (
                    [description] => Lunch
                )
            [1] => Array
                (
                    [duration] => 30
                )
            [3] => Array
                (
                    [description] => Lunch 2
                )
            [4] => Array
                (
                    [duration] => 30
                )
        )
)

通常应该是这样的地方:

Array
(
    [unpaid] => Array
        (
            [0] => Array
                (
                    [description] => Lunch
                    [duration] => 30
                )

            [1] => Array
                (
                    [description] => Lunch 2
                    [duration] => 30
                )

        )

)

唯一能解决这个问题的方法是,如果我用除以下数值之外的任何数字替换数字:

shifts.breaks.unpaid.b0.description
shifts.breaks.unpaid.b0.duration
shifts.breaks.unpaid.b1.description
shifts.breaks.unpaid.b1.duration

Array
(
    [unpaid] => Array
        (
            [b0] => Array
                (
                    [description] => Lunch
                    [duration] => 30
                )
            [b1] => Array
                (
                    [description] => Lunch 2
                    [duration] => 30
                )
        )
)

这是创建数组的函数:

function toArray($keys, $value){
    $array = array();
    $ref = &$array;
    while(count($keys) > 0){
        $n = array_shift($keys);
        if(!is_array($ref))
            $ref = array();
        $ref = &$ref[$n];
    }
    $ref = $value;
    return $array;
}

$keys包含$keys = explode('.', "my.testing.string");的位置,以及我一直在使用的示例:

$strings = array (
    "app.names.0.first"=> "Samuel",
    "app.names.0.last"=> "Smith",
    "app.names.1.first" => "Mary",
    "app.names.2.last" =>"Kubik"

);
$list = array();
foreach($strings as $key => $name) {
    $list[] = (toArray(explode('.', $key),$name));
}
print_r(call_user_func_array('array_merge_recursive', $list));

此时,我不太确定这是否与array_merge_recursive有关。任何纠正这个的帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

我找到的一个解决方案是重写一个新的array_merge_recursive函数而不覆盖数字键。

function array_merge_recursive_new() {

        $arrays = func_get_args();
        $base = array_shift($arrays);

        foreach ($arrays as $array) {
            reset($base); //important
            while (list($key, $value) = @each($array)) {
                if (is_array($value) && @is_array($base[$key])) {
                    $base[$key] = array_merge_recursive_new($base[$key], $value);
                } else {
                    $base[$key] = $value;
                }
            }
        }

        return $base;
  }

感谢php.net上的用户。当键是数字时,这将产生正确的输出。