php - cakePHP multidimensional array loop back to previous push -
i have created array need show list of projects current user. each project made of stages, each stage can have multiple kpis assigned it.
as can see stages appended array fine , part kpis.
the exception kpis not stage specific. when have type of kpi should assigned project section, spans multiple stages.
kpis assigned stages psid, if id null project wide kpi.
the issue i'm having project wide kpis. repeated, need each kpi appear once in array.
i've tried check pushed array element pushed 1 in order check.
$i=0; foreach($projects $project){ $projectstages = $this->projectstages->find('all', array( 'conditions' => array( 'pid' => $project['projects']['pid'] ) ) ); array_push($projects[$i], $projectstages); $n=0; foreach($projectstages $stage){ $projectkpis = $this->projectkpis->find('all', array( 'conditions' => array( 'npid' => $project['projects']['pid'] ) ) ); foreach($projectkpis $projectkpi){ if(empty($projectkpi['projectkpis']['npsid'])){ $n = $n-1; $curpkid = $projectkpi['projectkpis']['pkid']; if(isset($projects[$i]['projects'][$n]['projectkpis']['pkid'])){ $prevpkid = $projects[$i]['projects'][$n]['projectkpis']['pkid']; }else{ $prevpkid = null; } if($curpkid != $prevpkid){ echo 'cur: '.$curpkid.' - prev: '.$prevpkid; array_push($projects[$i]['projects'], $projectkpi); } }else if($projectkpi['projectkpis']['npsid'] == $stage['projectstages']['psid']){ array_push($projects[$i][$i][$n]['projectstages'], $projectkpi); } } $n++; } $i++; }
below code i'm using , array i'm getting back:
array ( [0] => array ( [projects] => array ( [pid] => 811 [name] => kpi project [description] => project has kpis [scheduledstartdate] => 2015-05-01 [scheduledenddate] => 2015-06-01 [actualstartdate] => [actualenddate] => [pmid] => 1137 [0] => array ( [projectkpis] => array ( [pkid] => 37 [npsid] => [type] => 1 [start] => 2015-05-03 [end] => 2015-05-09 [npid] => 811 [nkid] => 7 ) ) [1] => array ( [projectkpis] => array ( [pkid] => 37 [npsid] => [type] => 1 [start] => 2015-05-03 [end] => 2015-05-09 [npid] => 811 [nkid] => 7 ) ) [2] => array ( [projectkpis] => array ( [pkid] => 37 [npsid] => [type] => 1 [start] => 2015-05-03 [end] => 2015-05-09 [npid] => 811 [nkid] => 7 ) ) [3] => array ( [projectkpis] => array ( [pkid] => 37 [npsid] => [type] => 1 [start] => 2015-05-03 [end] => 2015-05-09 [npid] => 811 [nkid] => 7 ) ) [4] => array ( [projectkpis] => array ( [pkid] => 37 [npsid] => [type] => 1 [start] => 2015-05-03 [end] => 2015-05-09 [npid] => 811 [nkid] => 7 ) ) ) [pm] => array ( [first_name] => mr [last_name] => ) [0] => array ( [0] => array ( [projectstages] => array ( [psid] => 99 [pid] => 811 [stageid] => 1 [name] => 1 [description] => 1 [label] => 1 [actualstartdate] => 0000-00-00 [scheduledstartdate] => 2015-05-01 [actualenddate] => 0000-00-00 [scheduledenddate] => 2015-05-08 [prevstage] => n [keystage] => n [0] => array ( [projectkpis] => array ( [pkid] => 32 [npsid] => 99 [type] => 0 [start] => [end] => [npid] => 811 [nkid] => 2 ) ) ) ) [1] => array ( [projectstages] => array ( [psid] => 100 [pid] => 811 [stageid] => 2 [name] => 2 [description] => 2 [label] => 2 [actualstartdate] => 0000-00-00 [scheduledstartdate] => 2015-05-08 [actualenddate] => 0000-00-00 [scheduledenddate] => 2015-05-15 [prevstage] => n [keystage] => n [0] => array ( [projectkpis] => array ( [pkid] => 33 [npsid] => 100 [type] => 0 [start] => [end] => [npid] => 811 [nkid] => 4 ) ) ) ) [2] => array ( [projectstages] => array ( [psid] => 101 [pid] => 811 [stageid] => 3 [name] => 3 [description] => 3 [label] => 3 [actualstartdate] => 0000-00-00 [scheduledstartdate] => 2015-05-15 [actualenddate] => 0000-00-00 [scheduledenddate] => 2015-05-22 [prevstage] => n [keystage] => n [0] => array ( [projectkpis] => array ( [pkid] => 34 [npsid] => 101 [type] => 0 [start] => [end] => [npid] => 811 [nkid] => 5 ) ) ) ) [3] => array ( [projectstages] => array ( [psid] => 102 [pid] => 811 [stageid] => 4 [name] => 4 [description] => 4 [label] => 4 [actualstartdate] => 0000-00-00 [scheduledstartdate] => 2015-05-22 [actualenddate] => 0000-00-00 [scheduledenddate] => 2015-05-29 [prevstage] => n [keystage] => n [0] => array ( [projectkpis] => array ( [pkid] => 35 [npsid] => 102 [type] => 0 [start] => [end] => [npid] => 811 [nkid] => 6 ) ) ) ) [4] => array ( [projectstages] => array ( [psid] => 103 [pid] => 811 [stageid] => 5 [name] => 5 [description] => 5 [label] => 5 [actualstartdate] => 0000-00-00 [scheduledstartdate] => 2015-05-29 [actualenddate] => 0000-00-00 [scheduledenddate] => 2015-06-01 [prevstage] => n [keystage] => n [0] => array ( [projectkpis] => array ( [pkid] => 36 [npsid] => 103 [type] => 0 [start] => [end] => [npid] => 811 [nkid] => 8 ) ) ) ) ) ) [1] => array ( [projects] => array ( [pid] => 572 [name] => sgh [description] => dfgh [scheduledstartdate] => 2015-04-01 [scheduledenddate] => 2015-05-01 [actualstartdate] => [actualenddate] => [pmid] => 3304 ) [pm] => array ( [first_name] => mr [last_name] => brown ) [0] => array ( ) ) )
it seems retrieving same exact database results multiple times. i'm looking @ find in code block:
foreach($projectstages $stage){ $projectkpis = $this->projectkpis->find('all', array( 'conditions' => array( 'npid' => $project['projects']['pid'] ) ) ); // ... }
it looks me going same result set $projectkpis
on each iteration of foreach $projectstages
because find projectkpis
using project id , nothing whatever current $stage
is.
so every stage loop on same kpis again, causing logic convoluted trying avoid duplicates.
perhaps can simplified restructuring foreach loops. ditch outer foreach($projectstages $stage)
, iterate on kpis once. inside loop iterate on each stage when need test against $stage['projectstages']['psid']
, may unnecessary @ point.
Comments
Post a Comment