Welcome Guest, Not a member yet? Register   Sign In
Multi Dimensional array issue
#5

[eluser]fasfad[/eluser]
No Problem, it's all about making a picture in your head of the ouput you want and nest the array according to that picture.

Just something I thought about, I thought you may also want to sort the array according to the equipment usage per project.

So you might want to create another dimension, so change the code to get something like thisSadno redundant data)

Code:
Array
(
    [equipment] = Array //hold all equipment data in this array, you can check with isset() if it's already set before
    (
        [eq_id] =>Array({equipment_data}),
        [eq_id] =>Array({equipment_data}),
        [eq_id] =>Array({equipment_data}),
    )
    [projects] = Array //hold all your logs in this array grouped by projects
    (
        [pid] = Array
        (
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1),//here you log every hour per equipment in project, when you loop this data you can print equipment data by getting it from the equipment array: just using $myarray["equipment"][$current_eq_id] , or something
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1),
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1),
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1) //etc
        ),
        [pid] = Array
        (
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1),
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1),
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1),
            [eqid] = Array("2012-01-07"=>5,"2012-01-08"=>3,"2012-01-09"=>1) //etc
        )
    )
)



EDIT:
I changed some things to get the above output:
Code:
//some testdata
$obj1->eid = 54;
$obj1->make= "Titan";
$obj1->pid= NULL;
$obj1->hdate= '';
$obj1->hours= '';

$obj2->eid = 55;
$obj2->make= "Clark";
$obj2->pid= 1;
$obj2->hdate= '2012-11-07';
$obj2->hours= 5;

$obj3->eid = 55;
$obj3->make= "Clark";
$obj3->pid= 1;
$obj3->hdate= '2012-11-08';
$obj3->hours= 2;

$obj4->eid = 55;
$obj4->make= "Clark";
$obj4->pid= 1;
$obj4->hdate= '2012-11-09';
$obj4->hours= 4;

$obj5->eid = 55;
$obj5->make= "Clark";
$obj5->pid= 4;
$obj5->hdate= '2012-11-10';
$obj5->hours= 5;

$obj6->eid = 56;
$obj6->make= "Toyota";
$obj6->pid= 4;
$obj6->hdate= '2012-11-07';
$obj6->hours= 5;

$obj7->eid = 55;
$obj7->make= "Clark";
$obj7->pid= 4;
$obj7->hdate= '2012-11-10';
$obj7->hours= 2;


$testdata = array($obj1,$obj2,$obj3,$obj4,$obj5,$obj6,$obj7);

$output = array();
foreach($testdata as $result){
    //add equipment to eq (only if its not filled yet)
    if(!isset($output["equipment"][$result->eid])) {
        $output["equipment"][$result->eid]["make"] = $result->make;
//add other equipment data here
    }
    if($result->hdate == NULL || $result->hdate == '')
        continue;
    //add log to project for specific equipment
    if($result->pid == '' || $result->pid == NULL)
        $result->pid = 0;
    if(isset($output["projects"][$result->pid][$result->eid][$result->hdate])) {
        $output["projects"][$result->pid][$result->eid][$result->hdate] += $result->hours;
    } else {
        $output["projects"][$result->pid][$result->eid][$result->hdate] = $result->hours;
    }
}
var_dump($output);
outputs:
Code:
array(2) {
  ["equipment"]=>
  array(3) {
    [54]=>
    array(1) {
      ["make"]=>
      string(5) "Titan"
    }
    [55]=>
    array(1) {
      ["make"]=>
      string(5) "Clark"
    }
    [56]=>
    array(1) {
      ["make"]=>
      string(6) "Toyota"
    }
  }
  ["projects"]=>
  array(3) {
    [1]=>
    array(1) {
      [55]=>
      array(3) {
        ["2012-11-07"]=>
        int(5)
        ["2012-11-08"]=>
        int(2)
        ["2012-11-09"]=>
        int(4)
      }
    }
    [4]=>
    array(1) {
      [55]=>
      array(1) {
        ["2012-11-10"]=>
        int(7)
      }
    }
    [0]=>
    array(1) {
      [56]=>
      array(1) {
        ["2012-11-07"]=>
        int(5)
      }
    }
  }
}
Now i can use the above for nice output like this:
Code:
foreach($output["projects"] as $pid=>$project){

    echo "Project #$pid , equipment used:\n";
    foreach($project as $eid=>$logs){
        echo " - ".$output["equipment"][$eid]["make"]."\n";
        foreach($logs as $date=>$hours){
            echo "    -".$date.": ".$hours."hours\n";
        }
    }
}
Output will be:
Project #1 , equipment used:
- Clark
-2012-11-07: 5hours
-2012-11-08: 2hours
-2012-11-09: 4hours
Project #4 , equipment used:
- Clark
-2012-11-10: 7hours
- Toyota
-2012-11-07: 5hours


Messages In This Thread
Multi Dimensional array issue - by El Forum - 11-22-2012, 08:37 AM
Multi Dimensional array issue - by El Forum - 11-22-2012, 03:16 PM
Multi Dimensional array issue - by El Forum - 11-23-2012, 04:11 AM
Multi Dimensional array issue - by El Forum - 11-23-2012, 07:47 AM
Multi Dimensional array issue - by El Forum - 11-23-2012, 08:24 AM
Multi Dimensional array issue - by El Forum - 11-23-2012, 09:04 AM
Multi Dimensional array issue - by El Forum - 11-23-2012, 03:59 PM
Multi Dimensional array issue - by El Forum - 11-24-2012, 10:19 AM
Multi Dimensional array issue - by El Forum - 11-24-2012, 10:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB