动态填充多维数组 [英] Dynamically Populating Multi-Dimensional Arrays

查看:84
本文介绍了动态填充多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

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

I have the following array and function, which will create a multidimensional array based on array key's that you specific. For every attribute you pass to the function, it will add another dimension to the array. Think of it as array sorting.

提供的函数很好用,但是它使用eval,我很难想出一个一致的函数,没有它就不会出错.

The function supplied works great, but it uses eval, I had a hard time coming up with a function which was consistent and threw no errors without it.

让我们从数组开始:

$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
                    )

            )

    )

)

问题:通过相同的数组,您将以何种方式征服上面的方法以获得相同的结果?我很好奇其他人会怎么做.

The question: What are ways that you would conquer the above to obtain the same results form the same array? I am curious to see how others would do it.

谢谢

推荐答案

使用参考,我能够提出以下功能.效果很好!

Using references, I was able to come up with the below function. It works great!

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

It will automatically detect if multiple leafs exist at the end of the tree, and if so, branch, or leaf itself. Also, you can merge, using the $merge_classifier argument. This will allow you to merge columns called count for example, which will add the values together.

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

这篇关于动态填充多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆