动态填充多维数组

时间:2012-06-06 17:26:24

标签: php dynamic multidimensional-array

所有

我有以下数组和函数,它将根据您特定的数组键创建一个多维数组。对于传递给函数的每个属性,它将为数组添加另一个维度。把它想象成数组排序。

提供的功能效果很好,但它使用eval,我很难想出一个一致的功能,没有它就没有错误。

让我们从数组开始:

$array = array(
          array(‘name’ => ‘Person1’, ‘username’ => ‘username1’, ‘join_date’ => 12233445566, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person2’, ‘username’ => ‘username2’, ‘join_date’ => 12233445566, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person3’, ‘username’ => ‘username3’, ‘join_date’ => 12233445996, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person4’, ‘username’ => ‘username4’, ‘join_date’ => 12233445996, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person5’, ‘username’ => ‘username5’, ‘join_date’ => 12233445566, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person6’, ‘username’ => ‘username6’, ‘join_date’ => 12233445566, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person7’, ‘username’ => ‘username7’, ‘join_date’ => 12233445776, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person8’, ‘username’ => ‘username8’, ‘join_date’ => 12233445566, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person9’, ‘username’ => ‘username9’, ‘join_date’ => 12233445996, ‘state’ => ‘NJ’),
);

这是一个示例函数:

function createIndex($array, $index){
   $index_array = array();

   foreach($array as $result){

          if(is_array($index)){
                 $key = '$index_array';
                 for($i=0;$i<=sizeof($index)-1;$i++){
                       $key .= "['{$result[$index[$i]]}']";
                 }
                 $key .= "[]"; 
                 eval("$key = \$result;");
          }
          else{
                 $index_array[$result[$index]] = $result; 
          }
   }

   return $index_array;
}

通话功能:

print_r(create_index($array, array(‘state’, ‘join_date’)));

所需的输出:

Array
(
[NJ] => Array
    (
        [12233445566] => Array
            (
                [0] => Array
                    (
                        [name] => Person1
                        [username] => username1
                        [join_date] => 12233445566
                        [state] => NJ
                    )

                [1] => Array
                    (
                        [name] => Person2
                        [username] => username2
                        [join_date] => 12233445566
                        [state] => NJ
                    )

                [2] => Array
                    (
                        [name] => Person5
                        [username] => username5
                        [join_date] => 12233445566
                        [state] => NJ
                    )

            )

        [12233445996] => Array
            (
                [0] => Array
                    (
                        [name] => Person4
                        [username] => username4
                        [join_date] => 12233445996
                        [state] => NJ
                    )

                [1] => Array
                    (
                        [name] => Person9
                        [username] => username9
                        [join_date] => 12233445996
                        [state] => NJ
                    )

            )

    )

[NY] => Array
    (
        [12233445996] => Array
            (
                [0] => Array
                    (
                        [name] => Person3
                        [username] => username3
                        [join_date] => 12233445996
                        [state] => NY
                    )

            )

        [12233445566] => Array
            (
                [0] => Array
                    (
                        [name] => Person6
                        [username] => username6
                        [join_date] => 12233445566
                        [state] => NY
                    )

                [1] => Array
                    (
                        [name] => Person8
                        [username] => username8
                        [join_date] => 12233445566
                        [state] => NY
                    )

            )

        [12233445776] => Array
            (
                [0] => Array
                    (
                        [name] => Person7
                        [username] => username7
                        [join_date] => 12233445776
                        [state] => NY
                    )

            )

    )

)

问题:你有什么方法可以征服上述内容以从同一个数组中获得相同的结果?我很想知道其他人会怎么做。

由于

3 个答案:

答案 0 :(得分:1)

这就是我要做的。另请注意如何通过引用传入$ index_array。我个人在我的代码中使用它,因此我可以批量处理大量数据并偶尔取消设置数据。

所有关于通过引用存储变量:

function create_index($array, $indexes, &$index_array = array()) {
    if(!is_array($indexes)) $indexes = array($indexes);

    foreach($array as $result) {
        $object = &$index_array;

        foreach($indexes as $index){
            if(!array_key_exists((string)$result[$index], $object)) {
                $object[(string)$result[$index]] = array();
            }
            $object = &$object[(string)$result[$index]];
        }
        $object[] = $result;
    }

    return $index_array;
}

然后使用它:

// You can use it like you did in your question:
print_r(create_index($array, array('state', 'join_date')));

// Or you can process in chunks:

$results = array();

while(/* store data from database in $array */) {
    create_index($array, array('state', 'join_date'), &$results);
}

答案 1 :(得分:1)

我提出了与Ryan发布的基本相同的解决方案,但是当密钥是int时我遇到了问题...因此对数组密钥进行了string的类型转换...

function createIndex($set, $indexes)
{
    $return = array();

    if(!is_array($indexes)){
        $indexes = array($indexes);
    }

    foreach($set as $data){
        $curr =& $return;
        foreach($indexes as $index){
            $key  = (string)$data[$index];
            if(!is_array($curr[$key])){
                $curr[$key] = array();
            }
            $curr =& $curr[$key];
        }
        $curr[] = $data;
    }

    return $return;
}

答案 2 :(得分:0)

使用引用,我能够提出以下功能。它很棒!

它会自动检测树的末尾是否存在多个叶子,如果存在,则分支或叶子本身。此外,您可以使用$merge_classifier参数进行合并。这将允许您合并名为count的列,例如,它们会将值一起添加。

function createIndex($keys, Array $array, $merge_classifier = NULL, $return_array = array())
{               

    if(is_string($keys)){
        $tmp = $keys;
        $keys = array();
        $keys[] = $tmp; 
    }

    foreach($array as $result) {
        $object = &$return_array;

        foreach($keys as $index){
            if(!array_key_exists($result[$index], $object)) {
                $object[$result[$index]] = array();
            }
            $object = &$object[$result[$index]];
        }

        if(!is_null($merge_classifier)){
            $object = array_merge($result, array($merge_classifier => ($result[$merge_classifier] + $object[$merge_classifier])));
        }
        else{
            if(sizeof($object) > 0){
                $object[] = $result;    
            }else{
                $object = $result;  
            }
        }
    }

    return $return_array;
}