Topic: Multidimensional array dynamically

Hi,

   Think if I have a table as (id, field1, field2, field3, field4, field5) and I need to create a array with the key choose by the user.
   The user1 wants: $valuelist['field1']['field3'] = $value_line;
   The user2 wants: $valuelist['field2']['field4']['field5'] = $value_line;

    After I will show to user1
     field1
        field3 = values
 
    And user 2 will get
     field2
        field4
            field5 = values   

     Any idea how to do it?
     I thought about a recursive function....
     I also thought making a "ORDER BY fields_choose_by_user" and when it changes the values from any key choose print....

Best Regards,
Guilherme Filho

Re: Multidimensional array dynamically

Hi Guilherme,

I would like to help you but I do not understand.

$valuelist['field1']['field3'] = $value_line; This looks field1 is an array and field3 is the third element and $value_line is the value of [field1][field3]. Am I seeing it right?

Let me know.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Multidimensional array dynamically

Hi.
  I'm letting the user group the data as they want. Try this: table = (ID, DAY, MONTH, YEAR, CLIENT, INFORMATION, TOTAL)
  Maybe the user wants to check the register grouped by DAY - MONTH ... so I need to create a array [DAY][MONTH] maybe he wants by [CLIENT][MONTH][YEAR]...
  Creating this array I will print the registers by the way the user wants...
  The solution I found on the internet is use eval... also I found this class: http://demos.diafol.org/generic-table-r … -array.php

  But I'm really not sure what is the best option...

Re: Multidimensional array dynamically

I think I see it now...

Seems like you want to let the users select the fields they want in the order they want. Sounds like you need to display a page with the fields across the top with the options across in rows below then, server side you want to create the query based on these selections.

I'll play with it and get back.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

5 (edited by gui_calabria 2015-02-15 5:16:40 AM)

Re: Multidimensional array dynamically

Check where I'm:

For each (group_selected_by_user as $nomecampo)
{
    //I create a string doing
    $evalGrupo .= "[\$linha['".$nomecampo."']]";
}
The result is like like [$linha['cliente']][$linha['numeroos']]
So when I select the data I do:

while($linha = $result->fetch_assoc())
{
     eval('$dadosRelatorio'.$dadosLinha['agrupamento']['eval'].'[]=$linha;');
}

Now I have the follwing data:
$dadosRelatorio =>
Array
(
    [11] => Array
        (
            [54] => Array
                (
                    [0] => Array
                        (
                            [numeroos] => 54
                            [ret.1.2] => RM10002
                            [ret.1.3] => 2015-02-25
                            [ret.2.1] => 20
                            [ret.2.10] => Oficina - 54 - 1
                            [ret.2.12] =>
                            [ret.2.13] =>
                            [cliente] => 11
                        )
                )
            [55] => Array
                (
                    [0] => Array
                        (
                            [numeroos] => 55
                            [ret.1.2] => RM10002
                            [ret.1.3] => 2015-02-10
                            [ret.2.1] => 25
                            [ret.2.10] => Oficina - 55 - 1
                            [ret.2.12] =>
                            [ret.2.13] =>
                            [cliente] => 11
                        )
                    [1] => Array
                        (
                            [numeroos] => 55
                            [ret.1.2] => RM10002
                            [ret.1.3] => 2015-02-10
                            [ret.2.1] => 1
                            [ret.2.10] => Oficina - 55 - 2
                            [ret.2.12] =>
                            [ret.2.13] =>
                            [cliente] => 11
                        )
                )
        )
    [1] => Array
        (
            [56] => Array
                (
                    [0] => Array
                        (
                            [numeroos] => 56
                            [ret.1.2] => MIX5053
                            [ret.1.3] => 2015-03-05
                            [ret.2.1] =>
                            [ret.2.10] =>
                            [ret.2.12] =>
                            [ret.2.13] =>
                            [cliente] => 1
                        )
                )
        )
    [8] => Array
        (
            [57] => Array
                (
                    [0] => Array
                        (
                            [numeroos] => 57
                            [ret.1.2] => MIX5053
                            [ret.1.3] => 2015-03-05
                            [ret.2.1] => 100
                            [ret.2.10] => Oficina - 57 - 1
                            [ret.2.12] =>
                            [ret.2.13] =>
                            [cliente] => 8
                        )
                    [1] => Array
                        (
                            [numeroos] => 57
                            [ret.1.2] => MIX5053
                            [ret.1.3] => 2015-03-05
                            [ret.2.1] => 1
                            [ret.2.10] => Oficina - 57 - 2
                            [ret.2.12] =>
                            [ret.2.13] =>
                            [cliente] => 8
                        )
                )
        )
)

My next problem is how show this data, because in this case I have to have two:
foreach($array_1 as $result1)
    foreach($result1 as $result2)
       print information

But I can have more than two... or maybe less than 1.....

Re: Multidimensional array dynamically

I'm thinking a way o convert this array to a single array creating a key as:
11-54-0= array(information)
11-55-0= array(information)
11-55-1= array(information)
1-56-0= array(information)
8-57-0= array(information)
8-57-1= array(information)
so I could with a simple foreach print...
probably a recursive function could let me do it....

Re: Multidimensional array dynamically

It's been a waste what I'm doing... I grouped the data to make it flat again... It'a much easier use the field choose by the user adding on the SQL query by "ORDER BY ".field_1,field_2..field_N;

So when I'm going to print I can check when the field change to print the information/sub-total of the group....

Sorry to bother you!

Just to let someone know, to flat the array it can be done using the function
$flat = call_user_func_array('array_merge', $array);

Re: Multidimensional array dynamically

No bother. Always looking to learn something new.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.