I am completely new to programming and was tasked with changing the structure of an array, I just can't get it to work.
This is the original array:
[0] => Array
(
[att_values_id] => 5
[att_value] => Sloping
[att_id] => 5
[att_category] => Frame
)
[1] => Array
(
[att_values_id] => 13
[att_value] => Time Trial
[att_id] => 5
[att_category] => Frame
)
[2] => Array
(
[att_values_id] => 21
[att_value] => Mountain
[att_id] => 5
[att_category] => Frame
)
[3] => Array
(
[att_values_id] => 15
[att_value] => Carbon
[att_id] => 3
[att_category] => Material
)
[4] => Array
(
[att_values_id] => 15
[att_value] => Titanium
[att_id] => 9
[att_category] => Frame
)
[5] => Array
(
[att_values_id] => 15
[att_value] => Aluminum
[att_id] => 17
[att_category] => Frame
)
[6] => Array
(
[att_values_id] => 7
[att_value] => Expensive
[att_id] => 12
[att_category] => Price
)
[7] => Array
(
[att_values_id] => 7
[att_value] => Moderate
[att_id] => 33
[att_category] => Price
)
[8] => Array
(
[att_values_id] => 7
[att_value] => Entry Level
[att_id] => 40
[att_category] => Price
)
And I have to change it to this:
Array
(
[Frame] => Array
(
[5] => Sloping
[13] => Mountain
[21] => Time Trial
)
[Material] => Array
(
[3] => Carbon
[9] => Titanium
[17] => Aluminum
)
[Price] => Array
(
[12] => Expensive
[33] => Moderate
[40] => Entry Level
)
)
I have tried using foreach to go through the array but I don't know how to build the new array.
this little pseudocode is the closest we can get before plainly doing your homework.
foreach (firstarray as key => value) {
newarray[value['att_category']][value['att_id']]=value['att_value'];
}
Related
I have an reference array like this (the data amount is dynamic, about 3000 records)
Array
(
[0] => Array
(
[DEPT] => FIN
[KEYCODE] => AAAAA
[TYPE] => 7
)
[1] => Array
(
[DEPT] => SALE
[KEYCODE] => BBBBB
[TYPE] => 16
)
[2] => Array
(
[DEPT] => SCM
[KEYCODE] => CCCCC
[TYPE] => 1
)
)
and use the value of "TYPE" to search keys "[9][7][16][1]" and value of "KEYCODE" to search value of above keys in following array. (dynamic, about 5000 records)
Array
(
[0] => Array
(
[NAME] => Nick
[9] => GGGGG
[7] => AAAAA
[16] => MMMMM
[1] => KKKKK
)
[1] => Array
(
[NAME] => Chris
[9] => PPPPP
[7] => BBBBB
[16] => ZZZZZ
[1] => RRRRR
)
[2] => Array
(
[NAME] => Cathy
[9] => SSSSS
[7] => UUUUU
[16] => JJJJJ
[1] => AAAAA
)
[3] => Array
(
[NAME] => Allen
[9] => FFFFF
[7] => DDDDD
[16] => WWWWW
[1] => CCCCC
)
)
the output array should be
Array
(
[0] => Array
(
[NAME] => Chris
[9] => PPPPP
[7] => HHHHH
[16] => ZZZZZ
[1] => RRRRR
)
[1] => Array
(
[NAME] => Cathy
[9] => SSSSS
[7] => UUUUU
[16] => JJJJJ
[1] => XXXXX
)
)
"Nick" have item [7]=> AAAAA and "Allen" have item [1]=> CCCCC, so delete from data array.
I know the way to delete array item, but no idea to implement compare and search by key/value pair from other array.
For what I understand from your question, here's a simple solution for your required task:
$firstArr = array(
0 => array(
"DEPT" => 'FIN',
'KEYCODE' => 'AAAAA',
'TYPE' => 7
),
1 => array(
'DEPT' => 'SALE',
'KEYCODE' => 'BBBBB',
'TYPE' => 16
),
2 => array(
'DEPT' => 'SCM',
'KEYCODE' => 'CCCCC',
'TYPE' => 1
)
);
$secondArr = array(
0 => array(
'NAME' => 'Nick',
9 => 'GGGGG',
7 => 'AAAAA',
16 => 'MMMMM',
1 => 'KKKKK',
),
1 => array(
'NAME' => 'Chris',
9 => 'PPPPP',
7 => 'HHHHH',
16 => 'ZZZZZ',
1 => 'RRRRR'
),
2 => array(
'NAME' => 'Cathy',
9 => 'SSSSS',
7 => 'UUUUU',
16 => 'JJJJJ',
1 => 'XXXXX'
),
3 => array(
'NAME' => 'Allen',
9 => 'FFFFF',
7 => 'DDDDD',
16 => 'WWWWW',
1 => 'CCCCC'
)
);
foreach($firstArr as $row){
foreach($secondArr as $i => $que){
if(array_key_exists($row['TYPE'], $que) && $row['KEYCODE'] == $que[$row['TYPE']]){
unset($secondArr[$i]); // deleting selected item from the array...
}
}
}
echo '<pre>';
print_r($secondArr);
echo '</pre>';
Output of the above code:
Array
(
[1] => Array
(
[NAME] => Chris
[9] => PPPPP
[7] => HHHHH
[16] => ZZZZZ
[1] => RRRRR
)
[2] => Array
(
[NAME] => Cathy
[9] => SSSSS
[7] => UUUUU
[16] => JJJJJ
[1] => XXXXX
)
)
Is there any way to merge the array key with unique name, so i can get the unique key with their types and values. I am using yii2 and mysql
I am using the following SQL Query in yii2:
$data= $query->SELECT(["sum(no_of_pages) as pages_coded",'assigned_to','type_of_request','concat(firstname," ",lastname) as userfullname'])
->from('task')
->leftJoin('user','user.username = task.assigned_to')
->andWhere("sdlc_phase='Resolved'")
->orWhere("sdlc_phase='Semi_Resolved'");
$query->groupby(['assigned_to', 'type_of_request']);
//$query->->addGroupBy('assigned_to');
$query->orderby(['assigned_to'=>SORT_ASC]);
$data = $query->all();
and Output of SQL Query is
Array
(
[0] => Array
(
[pages_coded] => 5
[type_of_request] => Collector Tweak
[userfullname] => Sam
)
[1] => Array
(
[pages_coded] => 5
[type_of_request] => Collector Code
[userfullname] => John
)
[2] => Array
(
[pages_coded] => 42
[type_of_request] => Collector Tweak
[userfullname] => John
)
[3] => Array
(
[pages_coded] => 37
[type_of_request] => Clinical Tweak
[userfullname] => Dona
)
[4] => Array
(
[pages_coded] => 35
[type_of_request] => Collector Code
[userfullname] => Dona
)
[5] => Array
(
[pages_coded] => 7
[type_of_request] => Clinical Code
[userfullname] => Ricky
)
[6] => Array
(
[pages_coded] => 50
[type_of_request] => Clinical Tweak
[userfullname] => Ricky
)
[7] => Array
(
[pages_coded] => 4
[type_of_request] => Collector Code
[userfullname] => Ricky
)
[8] => Array
(
[pages_coded] => 17
[type_of_request] => Collector Tweak
[userfullname] => Ricky
)
)
However I want the generate the output like this, is there anyway to get this
Array
(
[Sam] => Array
(
[0] => Clinical Code
[1] => 0
[2] => Clinical Tweak
[3] => 0
[4] => Collector Code
[5] => 0
[6] => Collector Tweak
[7] => 5
)
[John] => Array
(
[0] => Clinical Code
[1] => 0
[2] => Clinical Tweak
[3] => 0
[4] => Collector Code
[5] => 5
[6] => Collector Tweak
[7] => 42
)
[Dona] => Array
(
[0] => Clinical Code
[1] => 0
[2] => Clinical Tweak
[3] => 37
[4] => Collector Code
[5] => 0
[6] => Collector Tweak
[7] => 35
)
[Ricky] => Array
(
[0] => Clinical Code
[1] => 7
[2] => Clinical Tweak
[3] => 50
[4] => Collector Code
[5] => 4
[6] => Collector Tweak
[7] => 17
)
)
help me sort out this issue.
For the output you want you will have to loop through the results creating another array.
$output = array();
foreach ($data as $row) {
$output[$row['userfullname']][$row['type_of_request']] = $row['pages_coded'];
}
Like this you will end up with arrays like:
$output = array(
'Sam' => array(
'Collector Tweak' => 5
),
);
And since you apparently want the other columns filled by 0 if they couldn't be found:
$categories = array(
'Clinical Code' => 0,
'Clinical Tweak' => 0,
'Collector Code' => 0,
'Collector Tweak' => 0,
);
foreach ($output as &$outputItem) {
$outputItem = array_merge($categories, $outputItem);
}
Which is, by definition, easier to track indexes and their respective values.
Tested here: https://3v4l.org/0k40Q
I have following multidimensional array
Array
(
[1] => 22
[2] => 12
[3] => Array
(
[0] => 4
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 6
[9] => 1
[10] => 2
[11] => 10
[12] => 0
[13] => 23
[14] => 18
[15] => 16
[16] => 19
[17] => 15
[18] => 5
[19] => 8
[20] => 11
[21] => 13
[22] => 17
[23] => 14
[24] => 20
)
I want to sort morethan one value below array from main array.
Array
(
[3] => Array
(
[0] => 4
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
)
Each key value compare with database column and sort an array. If not found value then array keep as it is.
For example:
The below key value is 21 have in database column then am expected results as,
[3] => Array
(
[0] => 21
[1] => 4
)
look as next array, the next array key value not found in the database then expected results as it is,
[5] => Array
(
[0] => 9
[1] => 3
)
The final output look for,
Array
(
[1] => 22
[2] => 12
[3] => Array
(
[0] => 21
[1] => 4
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 6
[9] => 1
[10] => 2
[11] => 10
[12] => 0
[13] => 23
[14] => 18
[15] => 16
[16] => 19
[17] => 15
[18] => 5
[19] => 8
[20] => 11
[21] => 13
[22] => 17
[23] => 14
[24] => 20
)
Could you please help me
Check this.
$dump_array=Array('1' => 2,'2' => 12,'3' => Array
('0' => 8,'1' => 21 ),
'5' => Array ('0' => 9,'1' => 3),
'7' => 7,
'8' => 64,
'9' => 15,
'10' => 22,
);
echo "<pre> Soure :"; print_r($dump_array);echo "</pre>";
sort($dump_array);
$result_array=array();
$array_unlist=array();
foreach($dump_array as $key=>$value){
if(is_array($value)){
sort($value);
array_push($array_unlist,$value);
}
else {array_push($result_array,$value);}
}
foreach($result_array as $index=>$val){
foreach($array_unlist as $key=>$digit){
if($val<$digit[0])
{
$res=$index.".5";
$result_array[$res]=$digit;
}
}
}
ksort($result_array);
echo "<pre> Result :"; print_r(array_values($result_array));echo "</pre>";
output:
Soure :Array
(
[1] => 2
[2] => 12
[3] => Array
(
[0] => 8
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 64
[9] => 15
[10] => 22
)
Result :Array
(
[0] => 2
[1] => Array
(
[0] => 3
[1] => 9
)
[2] => 7
[3] => Array
(
[0] => 8
[1] => 21
)
[4] => 12
[5] => 15
[6] => 22
[7] => 64
)
try this
foreach($array as $k => $v){
if(is_array($v)){
rsort($array[$k]);
}
}
print_r($array);
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.
I'm afraid that it is simply not possible what I'm trying to do, but I hope you can help me to find a nice way to solve this problem.
I've got the following PHP-array:
Array (
[0] => Array ( [article] => 10.499-1 [operation] => KN_KABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[1] => Array ( [article] => 10.499-1 [operation] => LAS_LABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[2] => Array ( [article] => 10.499-1 [operation] => ASS_HARNES [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[3] => Array ( [article] => 10.499-1 [operation] => CONTROLE [date] => 07-06-2013 [hours] => 0 [quantity] => 1 )
[4] => Array ( [article] => 24.030 [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[5] => Array ( [article] => 24.030 [operation] => ZAGEN-RAIL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[6] => Array ( [article] => 24.030 [operation] => KN_KABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[7] => Array ( [article] => 24.030 [operation] => ASS_RAIL [date] => 05-06-2013 [hours] => 0 [quantity] => 1 )
[8] => Array ( [article] => 791 070-6/GS/P [operation] => GS_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[9] => Array ( [article] => 791 070-6/GS/P [operation] => PR_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[10] => Array ( [article] => 791 070-6/GS/P [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[11] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => MAGAZIJN [date] => 10-06-2013 [hours] => 0 [quantity] => 1 )
[12] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => PR_UNIT [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
[13] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => LAB_PLAKKE [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
)
What I'm trying to do is to count for each date (key "datum") the sum of hours (in this example they are all 0 but I still want to do this because this will change in the future). What would be very practicle is to do a query like sql like SELECT SUM(hours) GROUP BY date but this is no SQL unfortunately.
Is there a way to group (and order) my array by a specific key (in this case "date") or, if not, is there an other way to get the result what I want?
EDIT
I recently added a key "department" which should be grouped by to. Thereby I do not only want to count the sum of "hours", but "quantity" too
Just create simply foreach for this.
$arr = array();
$sums = array();
foreach($arr as $k=>$v)
{
if(!isset($sums[$v['date']][$v['department']]['hours'])) $sums[$v['date']][$v['department']]['hours'] = 0;
if(!isset($sums[$v['date']][$v['department']]['quantity'])) $sums[$v['date']][$v['department']]['quantity'] = 0;
$sums[$v['date']][$v['department']]['hours'] += $v['hours'];
$sums[$v['date']][$v['department']]['quantity'] += $v['quantity'];
}
print_r($sums);
it will create array $sum where keys are your dates. If value doesn't exists it will add the value of hours to 0, else if it exists it will add to existing value.
edit
Fitted to OP needs.