I have an array from which I would like to extract some values in a particular form.
$array1 = array(
array("techArea01",1995),
array("techArea03",1996),
array("techArea01",1995),
array("techArea09",1998),
array("techArea09",1995),
array("techArea02",1997),
array("techArea01",1995),
array("techArea02",1999),
array("techArea09",1995)
);
But first I sort it by key = 0 and key = 1.
//Select indexes to sort array.
$sort = array();
foreach($array1 as $k=>$v) {
$sort['0'][$k] = $v['0']; //Sort by tech area.
$sort['1'][$k] = $v['1']; //Sort by priority date.
}
//These variables will be used independently below:
$sortedByTechArea = $array1;
$sortedByPriorityDate = $array1;
I sort first by tech area and exclude double entries:
//Sort by tech_area asc
array_multisort($sort['0'], SORT_ASC,$sortedByTechArea);
$sortedByTechArea = $sortedByTechArea;
//Exclude duplicated tech areas entries from array.
$excludeDoubleEntryInTechArea = array();
foreach ($sortedByTechArea as &$value) {
if (!isset($excludeDoubleEntryInTechArea[$value['0']]))
$excludeDoubleEntryInTechArea[$value['0']] =& $value;
}
$sortedByTechArea = array_values($excludeDoubleEntryInTechArea);
it give us following result:
$TechAreaWithoutDoubles:
Array
(
[0] => Array
(
[0] => techArea01
[1] => 1995
)
[1] => Array
(
[0] => techArea02
[1] => 1997
)
[2] => Array
(
[0] => techArea03
[1] => 1996
)
[3] => Array
(
[0] => techArea09
[1] => 1995
)
)
And finally I extract the unique tech areas:
$techArea = '';
foreach ($sortedByTechArea as $subArrayTechArea) {
$techArea .= "$subArrayTechArea[0]".',<br />';
}
echo 'Ordered list of tech areas:<br />';
echo $techArea,'</pre>';
And here the result:
Ordered list of tech areas:
techArea01,
techArea02,
techArea03,
techArea09,
To the years I do something similar and in the end I have following result:
Ordered list of years:
1995,
1996,
1997,
1998,
1999,
So far, so good, but what I really want is to know how many times a tech area occurs in a year. So I do the following:
echo '<pre>How many tech areas per year exist?<br />';
//Count of tech area per year.
$result = array();
foreach ($array1 as $part) {
$key = implode(', ', $part);
if( ! array_key_exists ($key, $result)) {
$result[$key] = 0;
}
$result[$key] = $result[$key] + 1;
}
foreach ($result as $key => $value) {
$count[] = $key.",".$value;
}
sort($count);
print_r($count);
echo '</pre>';
With that we have following result:
How many tech areas per year exist?
Array
(
[0] => techArea01, 1995,3
[1] => techArea02, 1997,1
[2] => techArea02, 1999,1
[3] => techArea03, 1996,1
[4] => techArea09, 1995,2
[5] => techArea09, 1998,1
)
So we know, for example, how many times tech area 01 occurs in 1995. The problem is that it doesn't tell us explicit when a tech area doesn't occur. As a matter of fact I'm searching a way to create following matrix:
techArea01 | techArea02 | techArea03 | techArea09
1995 3 | 0 | 0 | 2
1996 0 | 0 | 1 | 0
1997 0 | 1 | 0 | 0
1998 0 | 0 | 0 | 1
1999 0 | 1 | 0 | 0
Or to be more exact, following array would solve the problem:
$finalArray = array(
array(1995, 3, 0, 0, 2),
array(1996, 0, 0, 1, 0),
etc.
array(1999, 0, 1, 0, 0)
);
I thought I could use the ordered list that I create above or something similar to create the array, but I don't know how to insert the "zeros".
Is there any more elegant solution to do this? Or maybe someone has another idea to get the values direct from the original array?
I would appreciate any help.