这是两个相似的数组,我想按要求的方式加入。我怎样才能做到这一点。
$arr1 = array(
array(
'Class' => 'Class 1',
'Student' => array('Name one')
),
array(
'Class' => 'Class 2',
'Student' => array('Name one')
),
);
$arr2 = array(
array(
'Class' => 'Class 1',
'Student' => array('Name two, Name three')
),
array(
'Class' => 'Class 2',
'Student' => array('Name Two, Name Three, Name Four')
),
);
$result = array(
array(
'Class' => 'Class 1',
'Student' => array('Name one, Name two, Name three')
),
array(
'Class' => 'Class 2',
'Student' => array('Name one, Name Two, Name Three, Name Four')
),
);
两个数组包含相同数量的具有相同Key的数组。但关键价值是不同的。
答案 0 :(得分:1)
您可以在此使用array_map
,如下所示:
$arr1 = array(
array('Class' => 'Class 1','Student' => array('Name one')),
array('Class' => 'Class 2','Student' => array('Name one')),
);
$arr2 = array(
array('Class' => 'Class 1','Student' => array('Name two, Name three')),
array('Class' => 'Class 2','Student' => array('Name Two, Name Three, Name Four')),
);
$newList = [];
array_map(function($value, $list) use (&$newList) {
$newList[] = ['Class' => $value['Class'], 'Student' => array_merge($value['Student'], $list['Student'])];
}, $arr1, $arr2);
print_r($newList);
这将输出:
Array (
[0] => Array (
[Class] => Class 1
[Student] => Array ( [0] => Name one [1] => Name two, Name three )
)
[1] => Array (
[Class] => Class 2
[Student] => Array ( [0] => Name one [1] => Name Two, Name Three, Name Four )
)
)
正如@mickmackusa所提到的,你可能只希望将你的学生数组连接成一个字符串,
所以这就行了,
$arr1 = array(
array('Class' => 'Class 1','Student' => array('Name one')),
array('Class' => 'Class 2','Student' => array('Name one')),
);
$arr2 = array(
array('Class' => 'Class 1','Student' => array('Name two, Name three')),
array('Class' => 'Class 2','Student' => array('Name Two, Name Three, Name Four')),
);
$newList = [];
array_map(function($value, $list) use (&$newList) {
$newList[] = ['Class' => $value['Class'], 'Student' => array($value['Student'][0] . ', ' . $list['Student'][0])];
}, $arr1, $arr2);
print_r($newList);
这将输出一个连接数组元素,如下所示:
Array (
[0] => Array (
[Class] => Class 1
[Student] => Array ( [0] => Name one, Name two, Name three )
)
[1] => Array (
[Class] => Class 2
[Student] => Array ( [0] => Name one, Name Two, Name Three, Name Four )
)
)
答案 1 :(得分:0)
答案 2 :(得分:0)
这是您可以尝试的另一个用户定义的功能 -
function finalArray($array1, $array2){
$finalArray = []; #Final Array
foreach ($array1 as $key1 => $value) {
$finalArray[$key1]['Class'] = $value['Class'];
foreach($value['Student'] as $key2=>$class){
$ar1 = $array1[$key1]['Student'][$key2]; #Getting every Student Array Index from First Array
$ar2 = $array2[$key1]['Student'][$key2]; #Getting every Student Array Index from Second Array
$ar1 = explode(', ', $ar1); #String to Array
$ar2 = explode(', ', $ar2); #String to Array
$ar3 = array_merge($ar1,$ar2); #Merge Both Array
$ar3 = array_unique($ar3); #Getting Unique Array Index from Both Of Them
$ar3 = implode(', ', $ar3); #Array To String
$finalArray[$key1]['Student'][$key2] = $ar3; #Put in FinalArray
}
}
return $finalArray; #Return final Array
}
$finalArray = finalArray($arr1,$arr2);
注意:以上函数适用于相等的
[Student]
索引数组
输出
Array(
[0] => Array(
[Class] => Class 1
[Student] => Array
(
[0] => Name one, Name two, Name three
)
)
[1] => Array(
[Class] => Class 2
[Student] => Array
(
[0] => Name one, Name Two, Name Three, Name Four
)
)
)
答案 3 :(得分:0)
本周早些时候我解决了这个very, very similar question。 我的解决方案提供了重复学生值删除和可选的类和学生值排序的附加功能(尽管没有请求)。 我已经适应了这个案例:
代码:
Array(
[0] => Array(
[Class] => Class 1
[Student] => Array(
[0] => Name one, Name two, Name three
)
),
[1] => Array(
[Class] => Class 2
[Student] => Array(
[0] => Name one, Name Two, Name Three, Name Four
)
)
)
输出:
<?php
$options = [trim($m_o1), trim($m_o2), trim($m_o3), trim($m_o4), trim($m_o5)];
shuffle($a1);
$b1 = ['m_o1', 'm_o2', 'm_o3', 'm_o4' ,'m_o5'];
$result1 = array_combine($b1, $options);
extract($result1);
$sc_flag2= array();
$actual_flag2= array();
$correct_flag2= array();
if($m_c1=='1')
{
$correct_options = array(
$m_oo1,
' ',
' ',
' ',
' '
);
}
else if($m_c1=='2')
{
$correct_options = array(
$m_oo1,
$m_oo2,
' ',
' ',
' '
);
}
else if($m_c1=='3')
{
$correct_options = array(
$m_oo1,
$m_oo2,
$m_oo3,
' ',
' '
);
}
else if($m_c1=='4')
{
$correct_options = array(
$m_oo1,
$m_oo2,
$m_oo3,
$m_oo4,
' '
);
}
else if($m_c1=='5')
{
$correct_options = array(
$m_oo1,
$m_oo2,
$m_oo3,
$m_oo4,
$m_oo5
);
}
else
{
$correct_options = array('','','','','');
}
$selected_options = array(
$multiple_answer_1_1,
$multiple_answer_1_2,
$multiple_answer_1_3,
$multiple_answer_1_4,
$multiple_answer_1_5
);
if (in_array($m_o1, $correct_options) && in_array($correct_options, $selected_options)) {
array_push($sc_flag2, 1);
}
if (in_array($m_o2, $correct_options) && in_array($correct_options, $selected_options)) {
array_push($sc_flag2, 2);
}
if (in_array($m_o3, $correct_options) && in_array($correct_options, $selected_options)) {
array_push($sc_flag2, 3);
}
if (in_array($m_o4, $correct_options) && in_array($correct_options, $selected_options)) {
array_push($sc_flag2, 4);
}
if (in_array($m_o5, $correct_options) && in_array($correct_options, $selected_options)) {
array_push($sc_flag2, 5);
}
if (in_array($m_o1, $correct_options)) {
array_push($correct_flag2, 1);
}
if (in_array($m_o2, $correct_options)) {
array_push($correct_flag2, 2);
}
if (in_array($m_o3, $correct_options)) {
array_push($correct_flag2, 3);
}
array_push($actual_flag2, 1);
}
if (in_array($m_o2, $selected_options)) {
array_push($actual_flag2, 2);
}
if (in_array($m_o3, $selected_options)) {
array_push($actual_flag2, 3);
}
if (in_array($m_o4, $selected_options)) {
array_push($actual_flag2, 4);
}
if (in_array($m_o5, $selected_options)) {
array_push($actual_flag2, 5);
}
?>
<?php if (in_array(1, $sc_flag2)) { ?>
<label style="font-size:14px;background-color:#90EE90;border:1px solid green;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(A) <?php echo $m_o1; ?>
</label>
<?php } else if (in_array(1, $actual_flag2) && !in_array(1, $correct_flag2)) {
?>
<label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
<span class="">(A) <?php echo $m_o1; ?>
</label>
<?php } else if (in_array(1, $correct_flag2)) { ?>
<label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(A) <?php echo $m_o1; ?>
</label>
<?php } else {?>
<label style="font-size:14px">
<span class=""><input type="checkbox" value="<?php echo $m_o1; ?>" ></span> (A) <?php echo $m_o1; ?>
</label>
<?php }?><br/><br/>
<?php if (in_array(2, $sc_flag2)) {?>
<label style="font-size:14px;background-color:#90EE90;border:1px solid green;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(B) <?php echo $m_o2; ?>
</label>
<?php } else if (in_array(2, $actual_flag2) && !in_array(2, $correct_flag2)) {
?>
<label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
<span class="">(B) <?php echo $m_o2; ?>
</label>
<?php } else if (in_array(2, $correct_flag2)) {?>
<label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(B) <?php echo $m_o2; ?>
</label>
<?php } else {?>
<label style="font-size:14px">
<span class=""><input type="checkbox" value="<?php echo $m_o2; ?>" ></span> (B) <?php echo $m_o2; ?>
</label>
<?php }?><br/><br/>
<?php if (in_array(3, $sc_flag2)) {?>
<label style="font-size:14px;background-color:#90EE90;border:1px solid green;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(C) <?php echo $m_o3; ?>
</label>
<?php } else if (in_array(3, $actual_flag2) && !in_array(3, $correct_flag2)){
?>
<label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
<span class="">(C) <?php echo $m_o3; ?>
</label>
<?php } else if (in_array(3, $correct_flag2)){?>
<label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(C) <?php echo $m_o3; ?>
</label>
<?php } else {?>
<label style="font-size:14px">
<span class=""><input type="checkbox" value="<?php echo $m_o3; ?>" ></span> (C) <?php echo $m_o3; ?>
</label>
<?php }?><br/><br/>
<?php if (in_array(4, $sc_flag2)) {?>
<label style="font-size:14px;background-color:#90EE90;border:1px solid green;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(D) <?php echo $m_o4; ?>
</label>
<?php } else if (in_array(4, $actual_flag2) && !in_array(4, $correct_flag2)) {
?>
<label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
<span class="">(D) <?php echo $m_o4; ?>
</label>
<?php } else if (in_array(4, $correct_flag2)) {?>
<label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(D) <?php echo $m_o4; ?>
</label>
<?php } else {?>
<label style="font-size:14px">
<span class=""><input type="checkbox" value="<?php echo $m_o4; ?>" ></span> (D) <?php echo $m_o4; ?>
</label>
<?php }?><br/><br/>
<?php if (in_array(5, $sc_flag2)) {?>
<label style="font-size:14px;background-color:#90EE90;border:1px solid green;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(E) <?php echo $m_o5; ?>
</label>
<?php } else if (in_array(5, $actual_flag2) && !in_array(5, $correct_flag2)) {
?>
<label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
<span class="">(E) <?php echo $m_o5; ?>
</label>
<?php } else if (in_array(5, $correct_flag2)) {?>
<label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
<label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
<span class="">(E) <?php echo $m_o5; ?>
</label>
<?php } else {?>
<label style="font-size:14px">
<span class=""><input type="checkbox" value="<?php echo $m_o5; ?>" ></span> (E) <?php echo $m_o5; ?>
</label>
<?php }?>