1

全部,

我有以下数组和函数,它将根据您指定的数组键创建一个多维数组。对于传递给函数的每个属性,它都会向数组添加另一个维度。将其视为数组排序。

提供的函数很好用,但它使用 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
                    )

            )

    )

)

问题:有什么方法可以克服上述问题以从相同的数组中获得相同的结果?我很好奇其他人会怎么做。

谢谢

4

3 回答 3

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);
}
于 2012-06-06T18:03:01.990 回答
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;
}
于 2012-06-06T18:11:18.257 回答
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;
}
于 2012-06-26T19:01:05.433 回答