php中的数组排列
我的问题是我不能按我想要的结构排列数组。 array1和array2是动态生成的。正如您在数组2中所看到的,它有一个与数组1相同的subject,这意味着,该元素位于subject cpe 305之下。id为5的阵列2中的所有元素都属于cpe 305的主题。与cpe 304的逻辑相同 阵列1:php中的数组排列,php,Php,我的问题是我不能按我想要的结构排列数组。 array1和array2是动态生成的。正如您在数组2中所看到的,它有一个与数组1相同的subject,这意味着,该元素位于subject cpe 305之下。id为5的阵列2中的所有元素都属于cpe 305的主题。与cpe 304的逻辑相同 阵列1: Array ( [0] => Array ( [subjectid] => 5 [subjectcode] => Cpe 305 ) [
Array
(
[0] => Array
(
[subjectid] => 5
[subjectcode] => Cpe 305
)
[1] => Array
(
[subjectid] => 4
[subjectcode] => Cpe 304
)
)
Array
(
[0] => Array
(
[subjectid] => 5
[soid] => 1
[socode] => A
[sodesc] => Ability to apply knowledge of mathematics and science to solve engineering problems
)
[1] => Array
(
[subjectid] => 5
[soid] => 3
[socode] => C
[sodesc] => Ability to design a system, component, or process to meet the desired needs within realistic constraints such as economic, environmental, social, political, ethical, health and safety, manufacturability, and sustainability, in accordance to standards
)
[2] => Array
(
[subjectid] => 5
[soid] => 4
[socode] => D
[sodesc] => Ability to function on multidisciplinary teams
)
[3] => Array
(
[subjectid] => 5
[soid] => 5
[socode] => E
[sodesc] => Ability to identify, formulate, and solve engineering problems
)
[4] => Array
(
[subjectid] => 5
[soid] => 9
[socode] => I
[sodesc] => Recognition of the need for, and an ability to engage in life-long learning
)
[5] => Array
(
[subjectid] => 4
[soid] => 10
[socode] => J
[sodesc] => Knowledge of contemporary issues
)
阵列2:
Array
(
[0] => Array
(
[subjectid] => 5
[subjectcode] => Cpe 305
)
[1] => Array
(
[subjectid] => 4
[subjectcode] => Cpe 304
)
)
Array
(
[0] => Array
(
[subjectid] => 5
[soid] => 1
[socode] => A
[sodesc] => Ability to apply knowledge of mathematics and science to solve engineering problems
)
[1] => Array
(
[subjectid] => 5
[soid] => 3
[socode] => C
[sodesc] => Ability to design a system, component, or process to meet the desired needs within realistic constraints such as economic, environmental, social, political, ethical, health and safety, manufacturability, and sustainability, in accordance to standards
)
[2] => Array
(
[subjectid] => 5
[soid] => 4
[socode] => D
[sodesc] => Ability to function on multidisciplinary teams
)
[3] => Array
(
[subjectid] => 5
[soid] => 5
[socode] => E
[sodesc] => Ability to identify, formulate, and solve engineering problems
)
[4] => Array
(
[subjectid] => 5
[soid] => 9
[socode] => I
[sodesc] => Recognition of the need for, and an ability to engage in life-long learning
)
[5] => Array
(
[subjectid] => 4
[soid] => 10
[socode] => J
[sodesc] => Knowledge of contemporary issues
)
)
输出(我想要的结构)
我尝试过的
这是我正在测试的代码。我在测试代码中只包含很少的数据
$so = array();
$exist = array();
foreach ($this->subject as $key => $value) {
foreach ($this->sodetails as $key2 => $value2) {
if($value['subjectid'] === $value2['subjectid']){
$exist = array(
"subjectid" => $value['subjectid'],
"details" =>array(
"soid" => $value2['soid']
)
);
array_push($so, $exist);
}
}
}
与运行
mxn
次的循环/迭代不同,您可以做的是首先将subjectid
作为$arrray1
的键,并让自然PHP数组键处理完成这项工作
$new_array1 = array();
foreach($arrray1 as $item){
$new_array1[$item['subjectcode']] = $item;
}
foreach($array2 as $desc){
if(array_key_exists($desc['subjectid'],$new_array1){
$new_array1[$desc['subjectid']]['desc'][] = $desc;
}
}
这样,您只需进行
m+n
迭代。您只需在返回数组中将subject作为键传递
$exist = array();
foreach ($this->subject as $key => $value) {
foreach ($this->sodetails as $key2 => $value2) {
if($value['subjectid'] === $value2['subjectid']){
$exist[$value['subjectid']]['subjectid'] = $value['subjectid'];
$exist[$value['subjectid']]['subjectcode'] = $value['subjectcode'];
$exist[$value['subjectid']]['sodetails'] = array(array('soid'=>$value2['soid']),array('socode'=>$value2['socode']));
}
}
}
首先,您的输出数组不正确。一个数组不能有多个相同的索引,如“subjectid”、“subjectcode”。我们需要一个不同的索引。明智的做法是使用subjectid作为外部数组的索引 首先准备外部阵列,因为它是阵列1
foreach($this->subject as $key => $val){
$myArray[$val['subjectid']]['subjectid'] => $val['subjectid'];
$myArray[$val['subjectid']]['subjectcode'] => $val['subjectcode'];
}
现在迭代你的sodetails数组
foreach($this->sodetails as $key => $val){
$temp['soid'] = $val['soid'];
$temp['socode'] = $val['socode'];
array_push($myArray[$val['subjectid']]['sodetails'], $temp);
}
还有其他的方法。但这是我的风格,我相信它会解决你的问题。你试过写代码吗?让我们看看你在解决这个问题上的努力。