Dynamically Populating Multi-Dimensional Arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi All,

 

I was working on a project and needed to dynamically populate a
multi-dimensional array based on the size of an array. The script does
exactly what I want it to do, but I can't imagine that it's done in the
best way. I used an eval to construct the array based on the number of
values passed to the index array. When attempting to do the same with
Variable Variables, the addition of brackets caused the PHP Parser to
die.

 

Does anyone have any suggestions or alternatives to achieving the same
results?

 

Thanks,

 

Mike

 

Array:

 

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

 

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;

}

 

 

Calling Function:

 

print_r(create_index($array, array('state', 'join_date')));

 

 

Output:

 

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

                        )

 

                )

 

        )

 

)

 


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux