0

I have one array something like this

Array
(
 [0] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        [module_name] => Design
        [project_module_time] => 150
        [allocated_time] => 50
    )

[1] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        [module_name] => Coding
        [project_module_time] => 250
        [allocated_time] => 200
    )

[2] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        [module_name] => Testing
        [project_module_time] => 100
        [allocated_time] => 50
    )

[3] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        [module_name] => Design
        [project_module_time] => 200
        [allocated_time] => 100
    )

[4] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        [module_name] => Coding
        [project_module_time] => 200
        [allocated_time] => 100
    )

[5] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        [module_name] => Testing
        [project_module_time] => 200
        [allocated_time] => 100
    )
)

And i want result something like this

Array
(
[0] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        Array
        (
            [0] => Array
                (
                    [module_name] => Design
                    [project_module_time] => 150
                    [allocated_time] => 50
                )
             [1] => Array
                (
                    [module_name] => Coding
                    [project_module_time] => 250
                    [allocated_time] => 200
                )
             [2] => Array
                (
                    [module_name] => Testing
                    [project_module_time] => 100
                    [allocated_time] => 50
                )       
        )
    )

[1] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        Array
        (
            [0] => Array
                (
                    [module_name] => Design
                    [project_module_time] => 200
                    [allocated_time] => 100
                )
             [1] => Array
                (
                    [module_name] => Coding
                    [project_module_time] => 200
                    [allocated_time] => 100
                )
             [2] => Array
                (
                    [module_name] => Testing
                    [project_module_time] => 200
                    [allocated_time] => 100
                )
        )
    )
)

I don't know how to make this type of array , so please help me to solve this problem. Your help would be appreciated.

5 Answers 5

1

Looks like what you want to do is actually run through your array and group it according to project.

This is easy enough if you set up an associated array that lets you lookup your projects.

$grouped_data   = array();
foreach ($data as $item) {
    //we'll used the project name as the key
    $key        = $item['project_name'];
    if (empty($grouped_data[$key])) {
        //this project isn't created yet, create it
        $grouped_data[$key]         = $item;
        $grouped_data[$key]['modules']  = array(); //create the module sub array to store things
    }
    $grouped_data[$key]['modules'][]        = $item; //store your module for this project 
}

Of course you'll need to do some work of your own to clean up what you store in the sub array. Currently it stores everything so some keys are duplicated. But this should get you started without a problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for response I have tried your solution but still its not return array which i want and i mentioned in question.
Not sure how they don't match up, so I can't really help you anymore unless you give me more information.
Update your question with what you tried, and how it is differing from what you are expecting.
0

Just like you could save a variable inside an array, you also can save an array inside an array:

$someArray = array(
    'Value 1',
    array(
        array(
            'Some more value,
            'more'
        ),
        array(
            'Some more value,
            'more'
        )
    )
);

Comments

0

Make sure you will have some index for your modules

$myArray = Array();    

$myArray[0] = Array(    
    "employee_id" => 2,    
    "project_name" => "WeeFavR",    
    "project_planned_hour" => 500,    
    "modules" => Array(    
        Array(    
            "module_name" => "Design",    
            "project_module_time" => 150,    
            "allocated_time" => 50    
        ),    
    ),    
);    

Comments

0

What you can do is something like this:

    $arr_project = array ();
    $arr_example_tmp =  array ("module_name" => "Design",
                        "project_module_time" => 200,
                        "allocated_time" => 100);


    $arr_project["project_planned_hour"][] = $arr_example_tmp;


    $arr_example_tmp =  array ("module_name" => "Coding",
                        "project_module_time" => 200,
                        "allocated_time" => 100);

   $arr_project["project_planned_hour"][] = $arr_example_tmp;

1 Comment

what isn't? I did a little edit because I missed some quotes. When I var_dump($arr_project) it's a filled array
0
$out = array();
foreach ($arr as $key => $value){
    $dex = $value['employee_id'].' - '.$value['project_name'];
    if ( ! isset($out[$dex])){
        $out[$dex] = array_slice($value, 0, 3);
    }
    $out[$dex]['details'][] = array_slice($value, 3);
}
$out = array_values($out);
print_r($out);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.