This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I want to sort array by value P if two P are same then sort by PD if two PD is same then sort by PF same as PA.
How i achieve this?
I have Multidimensional array In PHP Like
PHP:
$abc = array(
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '2','L' => '0','D'=> '0','PF' => '2904','PA' => '1932','PD' => '972','P' => '6')),
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '2320','PA' => '1974','PD' => '346','P' => '3')),
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '1620','PA' => '1824','PD' => '-204','P' => '3')),
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '0','L' => '2','D'=> '0','PF' => '2200','PA' => '2300','PD' => '-100','P' => '0'))
);
HTML :
Array
(
[0] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 0
[L] => 2
[D] => 0
[PF] => 2200
[PA] => 2300
[PD] => -100
[P] => 0
)
)
[1] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 2320
[PA] => 1974
[PD] => 346
[P] => 3
)
)
[2] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 1620
[PA] => 1824
[PD] => -204
[P] => 3
)
)
[3] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 2
[L] => 0
[D] => 0
[PF] => 2904
[PA] => 1932
[PD] => 972
[P] => 6
)
)
)
Want similar like this.
Array
(
[0] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 2
[L] => 0
[D] => 0
[PF] => 2904
[PA] => 1932
[PD] => 972
[P] => 6
)
)
[1] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 2320
[PA] => 1974
[PD] => 346
[P] => 3
)
)
[2] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 1620
[PA] => 1824
[PD] => -204
[P] => 3
)
)
[3] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 0
[L] => 2
[D] => 0
[PF] => 2200
[PA] => 2300
[PD] => -100
[P] => 0
)
)
)
I have tried like this.
$memberArray1 = usort($memberArray, function($a, $b) {
return $b['overall']['P'] - $a['overall']['P'];
});
$memberArray1 = usort($memberArray, function($a, $b) {
return $b['overall']['PD'] - $a['overall']['PD'];
});
But the issue is when i sort like P then it works but after when i sort like PD then it sort by PD not like P.
You can use uasort along with rsort as
uasort($abc,function($a,$b){
$c = $a['P'] - $b['P'];
$c .= $a['PD'] - $b['PD'];
$c .= $a['PF'] - $b['PF'];
$c .= $a['PA'] - $b['PA'];
return $c;
});
array_reverse($abc);
print_r($abc);
Demo
Related
I'm stuck on this problem and I hope someone can help me on that.
I have an array which I want to group by a specific key. The only problem is that I want to have a new array whenever the value of the key changes during the loop. Here is an example of the array.
Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[2] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
[3] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[5] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[6] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
I did
foreach($report_items as $item)
{
$groupedItems[$item['grouped_by']][] = $item;
}
and I got
Array
(
[1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
)
What I'm actually looking for is something like this, where the arrays are separated during the loop and a suffix is added to the key whenever the value changes.
Array
(
[1.1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4.1] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
[1.2] => Array
(
[0] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
)
I'm not really aware of a simple way to solve this, so I would appreciate any help. Thank you!
Not going to bother to recreate your full array here, I am using a reduced version with just the id and the grouped_by value, but the principle is of course exactly the same if you do it with your "full" array.
I am using a counter array to keep track of which "iteration" of a specific group we are currently dealing with. In classic control break implementation logic, I am comparing the grouped_by of the current record, with that of the previous one - if those differ, then the counter for this grouped_by value has to be incremented by 1. And then, I am simply creating the array key to use, by combining the grouped_by value, and the current count for it.
$data = [['id' => 972, 'grouped_by' => 1], ['id' => 664, 'grouped_by' => 4], ['id' => 631, 'grouped_by' => 4], ['id' => 630, 'grouped_by' => 1], ['id' => 971, 'grouped_by' => 1], ['id' => 973, 'grouped_by' => 1], ['id' => 974, 'grouped_by' => 1]];
$grouped_result = $grouped_by_counts = [];
$previous_grouped_by = null; // a value that will never occur in the data
foreach($data as $datum) {
if($datum['grouped_by'] !== $previous_grouped_by) {
$grouped_by_counts[$datum['grouped_by']] =
isset($grouped_by_counts[$datum['grouped_by']]) ?
$grouped_by_counts[$datum['grouped_by']] + 1 : 1;
}
$grouped_result[
$datum['grouped_by'].'.'.$grouped_by_counts[$datum['grouped_by']]
][] = $datum;
$previous_grouped_by = $datum['grouped_by'];
}
print_r($grouped_result);
Live example: https://3v4l.org/odtie
Hope you can help me with this. Because I'm trying to reorder them but i need first to make the be at a single level of array. From associative array to single array.
$MY_ASSOC_ARRAY
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[1] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)
)
[1] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
)
)
)
)
Alright now the array should not be in that multi level (associative) array instead it will look like this:
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)
[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)
[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)
[3] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[4] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
[5] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
I'm no sure how will that be possible in the most effecient way without using too much memory that will affect the speed with the use of Php Codeigniter. Thanks!
[UPDATE # 1]
here are the code that I have tried but the order is different
foreach($tree as $key => $value) {
$single[] = $value;
}
And this is the output for this failed attemp...
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)
[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)
[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)
[3] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
[4] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[5] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)
The [NAME] => companion should be at the last array not on 4th ([3] => Array)
UPDATE # 2:
Feel bad about the down votes... if this question or problem is not useful on your end
<?php
$array = Array(
0 => Array
(
'MAIN_ID' => 1,
'ORDER' => 1,
'NAME' => 'Animal',
'PARENT_ID' => 0,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 4,
'ORDER' => 4,
'NAME' => 'doggie',
'PARENT_ID' => 1,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 18,
'ORDER' => 18,
'NAME' => 'hunting',
'PARENT_ID' => 4,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 21,
'ORDER' => 21,
'NAME' => 'setter',
'PARENT_ID' => 18,
),
1 => Array
(
'MAIN_ID' => 22,
'ORDER' => 22,
'NAME' => 'pointer',
'PARENT_ID' => 18,
)
)
),
1 => Array
(
'MAIN_ID' => 19,
'ORDER' => 19,
'NAME' => 'companion',
'PARENT_ID' => 4,
)
)
)
)
)
);
$out = [];
$out = generateArray($array, $out);
print_r($out);
function generateArray($in, $out){
foreach($in as $value){
$childs = false;
if(isset($value['childs'])){
$childs = $value['childs'];
unset($value['childs']);
}
$out[] = $value;
if($childs)
$out = generateArray($childs, $out);
}
return $out;
}
?>
I am unable to get stores and store_id from this array. Please help me, how to get stores and store_id from this array of arrays?
Array
(
[group_data] => Uni_Banner_Model_Bannergroup Object
(
[_eventPrefix:protected] => core_abstract
[_eventObject:protected] => object
[_resourceName:protected] => banner/bannergroup
[_resource:protected] =>
[_resourceCollectionName:protected] => banner/bannergroup_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[group_id] => 4
[group_name] => list banner1
[group_code] => list_banner1
[banner_width] => 320
[banner_height] => 460
[animation_type] => 1
[banner_effects] => Fade/Appear
[pre_banner_effects] =>
[banner_ids] => 1
[show_title] => 0
[show_content] => 0
[link_target] => 0
[status] => 1
[created_time] => 2015-03-04 14:47:40
[update_time] => 2015-03-04 14:47:40
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[group_id] => 4
[group_name] => list banner1
[group_code] => list_banner1
[banner_width] => 320
[banner_height] => 460
[animation_type] => 1
[banner_effects] => Fade/Appear
[pre_banner_effects] =>
[banner_ids] => 1
[show_title] => 0
[show_content] => 0
[link_target] => 0
[status] => 1
[created_time] => 2015-03-04 14:47:40
[update_time] => 2015-03-04 14:47:40
)
[_idFieldName:protected] => group_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
[banner_data] => Array
(
[0] => Uni_Banner_Model_Banner Object
(
[_eventPrefix:protected] => core_abstract
[_eventObject:protected] => object
[_resourceName:protected] => banner/banner
[_resource:protected] =>
[_resourceCollectionName:protected] => banner/banner_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[banner_id] => 1
[title] => List Banner
[filename] => custom/banners/File-1440417903.jpg
[link] => http://localhost/magentonew/french/
[banner_content] =>
[stores] => 4,6
[storeviews] =>
[status] => 1
[sort_order] => 0
[banner_type] => 0
[created_time] => 2015-08-25 16:44:46
[update_time] => 2015-08-25 16:44:46
[store_id] => Array
(
[0] => 4
[1] => 6
)
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[banner_id] => 1
[title] => List Banner
[filename] => custom/banners/File-1440417903.jpg
[link] => http://localhost/magentonew/french/
[banner_content] =>
[stores] => 4,6
[storeviews] =>
[status] => 1
[sort_order] => 0
[banner_type] => 0
[created_time] => 2015-08-25 16:44:46
[update_time] => 2015-08-25 16:44:46
[store_id] => Array
(
[0] => 4
[1] => 6
)
)
[_idFieldName:protected] => banner_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
)
)
This is the code get the data for this specific array and objects. You should really use some getter methods for the objects and iterate the array(s) properly.
$arr = your array
$arr['banner_data'][0]->_data['stores'] and
$arr['banner_data'][0]->_data['store_id'][0]
$arr['banner_data'][0]->_data['store_id'][1]
I have created an array which I need to show a list of projects for the current user. Each project is made up of stages, each stage can have multiple KPIs assigned to it.
As you can see the stages are appended to the array fine and for the most part so are the KPIs.
The exception to this is with KPIs that are not stage specific. When I have this type of KPI is should be assigned to the project section, as it spans multiple stages.
KPIs are assigned to stages by the PSID, if the id is null then it is a project wide KPI.
The issue I'm having is with the project wide KPIs. They are repeated, I need each KPI to appear once in the array.
I've tried to check the previously pushed array element with the currently pushed one in order to check.
$i=0;
foreach($projects as $project){
$projectStages = $this->ProjectStages->find('all',
array(
'conditions' => array(
'pid' => $project['Projects']['pid']
)
)
);
array_push($projects[$i], $projectStages);
$n=0;
foreach($projectStages as $stage){
$projectKPIs = $this->ProjectKPIs->find('all',
array(
'conditions' => array(
'nPid' => $project['Projects']['pid']
)
)
);
foreach($projectKPIs as $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 is the code I'm using and the array I'm currently getting back:
Array
(
[0] => Array
(
[Projects] => Array
(
[pid] => 811
[name] => Kpi Project
[description] => This 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] => A
)
[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 that you are retrieving the same exact database results multiple times. I'm looking at the find in this code block:
foreach($projectStages as $stage){
$projectKPIs = $this->ProjectKPIs->find('all',
array(
'conditions' => array(
'nPid' => $project['Projects']['pid']
)
)
);
// ...
}
It looks to me that you are going to get the same result set for $projectKPIs on each iteration of the foreach $projectStages because the find for ProjectKPIs is using the project id and nothing to do with whatever the current $stage is.
So effectively for every stage you then loop over all the same KPIs again, causing your logic to get convoluted trying to avoid duplicates.
Perhaps this can be simplified by restructuring your foreach loops. I would ditch the outer foreach($projectStages as $stage) and then iterate over all the KPIs only once. Then inside that loop iterate over each stage only when you need to test against the $stage['ProjectStages']['psid'], which may actually be unnecessary at that point.
This is the cart array in the Virtuemart 2.0.22a ..
this is stored in "$cart"
how to print [virtuemart_product_id] => 21 at 4.
$cart=
VirtueMartCart Object
(
[products] => Array
(
[21] => stdClass Object
(
[virtuemart_manufacturer_id] => Array
(
)
[slug] => stylish-shirt
[published] => 1
[virtuemart_product_price] =>
[virtuemart_product_id] => 21
[virtuemart_shoppergroup_id] =>
[product_price] => 450.00000
[override] => 0
[product_override_price] => 0.00000
[product_tax_id] => -1
[product_discount_id] => -1
[product_currency] => 68
[virtuemart_vendor_id] => 1
[product_parent_id] => 0
[product_sku] =>
[product_name] => Stylish Shirt
[product_s_desc] =>
[product_weight] =>
[product_weight_uom] => KG
[product_length] =>
[product_width] =>
[product_height] =>
[product_lwh_uom] => M
[product_in_stock] => 0
[product_ordered] => 0
[product_sales] => 0
[product_unit] => KG
[product_packaging] =>
[min_order_level] =>
[max_order_level] =>
[virtuemart_media_id] => Array
(
[0] => 21
)
[step_order_level] =>
[image] => VmImage Object
(
[media_attributes] => 0
[setRole] =>
[file_name] => 3
[file_extension] => jpg
[virtuemart_media_id] => 21
[_foldersToTest:VmMediaHandler:private] => Array
(
[0] => E:\wamp\www\ecomm\images\stories\virtuemart\product\
[1] => E:\wamp\www\ecomm\images\stories\virtuemart\product\resized\
)
[_actions:VmMediaHandler:private] => Array
(
)
[_mLocation:VmMediaHandler:private] => Array
(
)
[_hidden:VmMediaHandler:private] => Array
(
)
[theme_url] => http://localhost/ecomm/components/com_virtuemart/
[virtuemart_vendor_id] => 1
[file_title] => 3.jpg
[file_description] =>
[file_meta] =>
[file_mimetype] => image/jpeg
[file_type] => product
[file_url] => images/stories/virtuemart/product/3.jpg
[file_url_thumb] => images/stories/virtuemart/product/resized/3_90x90.jpg
[published] => 1
[file_is_downloadable] => 0
[file_is_forSale] => 0
[file_is_product_image] => 0
[shared] => 0
[file_params] =>
[file_lang] =>
[_translatable] =>
[_tablePreFix] =>
[created_on] => 2013-08-12 12:32:19
[created_by] => 572
[modified_on] => 2013-08-12 12:38:50
[modified_by] => 572
[file_url_folder] => images/stories/virtuemart/product/
[file_path_folder] => images\stories\virtuemart\product\
[file_url_folder_thumb] => images/stories/virtuemart/product/resized/
[media_role] => file_is_displayable
[file_name_thumb] => 3_90x90
)
[categories] => Array
(
[0] => 6
)
[virtuemart_category_id] => 6
[category_name] => Shirts
[link] => /ecomm/index.php/component/virtuemart/shirts/stylish-shirt-detail?Itemid=0
[packaging] =>
[quantity] => 1
)
)
[_inCheckOut] =>
[_dataValidated] =>
[_blockConfirm] =>
[_confirmDone] =>
[_redirect] =>
[_redirect_disabled] =>
[_lastError] =>
[vendorId] => 1
[lastVisitedCategoryId] => 0
[virtuemart_shipmentmethod_id] => 0
[virtuemart_paymentmethod_id] => 1
[automaticSelectedShipment] => 1
[automaticSelectedPayment] => 1
[BT] => 0
[ST] => 0
[tosAccepted] =>
[customer_comment] =>
[couponCode] =>
[order_language] =>
[cartData] => Array
(
[VatTax] => Array
(
)
[duty] => 1
[payment] => 0
[paymentName] =>
Finally i got the output.. after lot of struggles
foreach($cart->products as $cur)
{
echo $cur->virtuemart_product_id;
}