我有幾個像這樣的關聯陣列
$arrayOne = array (
"name" => 'john',
"position" => 'instructor',
"hired" => '2010',
"department" => 'math',
);
$arrayTwo = array (
"name" => 'smith',
"position" => 'instructor',
"hired" => '2010',
"department" => 'science',
);
$arrayThree = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2009',
"department" => 'math',
);
$arrayFour = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2011',
"department" => 'math',
);
我想獲取最新雇用年份的陣列,并且還基于部門訂單。
所以部門優先級是['math', 'science', 'social'](根據優先級排序)
如果只有一個陣列有最近幾年: 例如從上面的陣列中我們只得到這個:
$arrayFour = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2011',
"department" => 'math',
);
因為這個是唯一一個最近一年錄用的,所以我們不關心這個部門。
但是如果有多個陣列同年,那么我們看部門。
$arrayOne = array (
"name" => 'john',
"position" => 'instructor',
"hired" => '2010',
"department" => 'math',
);
$arrayTwo = array (
"name" => 'smith',
"position" => 'instructor',
"hired" => '2010',
"department" => 'science',
);
$arrayThree = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2010',
"department" => 'math',
);
$arrayFour = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2010',
"department" => 'social',
);
由于我們有2010招聘年份的陣列,我想獲得math部門下的陣列。由于數學在系中排在第一位,如果沒有數學,我們會得到科學的,如果沒有科學,那么社會。在這個例子中,我們只會得到$arrayOne和$arrayThree。
我從創建一個函式開始,但我很困惑如何檢查年份和部門
function instructors($arrays){
foreach ($arrays as $array){
// how to check for recent year?
if($array['hired']){
}
}
}
var_dump(instructors($arrayOne, $arrayTwo, $arrayThree, $arrayFour ));
uj5u.com熱心網友回復:
我撰寫了以下代碼:
該getResult方法首先檢查最近一年是否有一項,如果有,則回傳這一項。如果不是,它將根據部門順序遍歷陣列并回傳存在的第一個專案。
$arrayOne = array (
"name" => 'john',
"position" => 'instructor',
"hired" => '2010',
"department" => 'math',
);
$arrayTwo = array (
"name" => 'smith',
"position" => 'instructor',
"hired" => '2010',
"department" => 'science',
);
$arrayThree = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2010',
"department" => 'math',
);
$arrayFour = array (
"name" => 'dave',
"position" => 'instructor',
"hired" => '2010',
"department" => 'social',
);
function getLatestHiredYear(...$arrays) : int
{
return (int) max(array_column($arrays, "hired"));
}
function getArraysBasedOnDepartment(int $year, string $department, ...$arrays) : array
{
$return = [];
foreach($arrays as $array) {
if($array['department'] == $department && $array['hired'] == $year) {
$return[] = $array;
}
}
return $return;
}
function getResult(...$arrays) : array
{
$orderItems = ['math', 'science', 'social'];
$return = [];
$recentYear = getLatestHiredYear(...$arrays);
$grouped = [];
foreach($arrays as $array) {
$grouped[$array['hired']][] = $array;
}
//only one of recent year
if(count($grouped[$recentYear]) === 1) {
return $grouped[$recentYear];
}
//Multiple items for year
foreach($orderItems as $department) {
$departmentArrays = getArraysBasedOnDepartment($recentYear, $department, ...$arrays);
if(!empty($departmentArrays)) {
$return = $departmentArrays;
break;
}
}
return $return;
}
var_dump(getResult($arrayOne, $arrayTwo, $arrayThree, $arrayFour));
輸出:
array(2) {
[0]=>
array(4) {
["name"]=>
string(4) "john"
["position"]=>
string(10) "instructor"
["hired"]=>
string(4) "2010"
["department"]=>
string(4) "math"
}
[1]=>
array(4) {
["name"]=>
string(4) "dave"
["position"]=>
string(10) "instructor"
["hired"]=>
string(4) "2010"
["department"]=>
string(4) "math"
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349355.html
上一篇:將PHP陣列值放入新陣列
