[英]how to push values in multidimensional array in php
$classType
array i am storing the class wise type like class 1 having type A & class 2 having type is A & class 3 having type is B $classType
数组我正在存储类明智的类型,例如具有类型 A 的类 1 和具有类型的类 2 是 A 和具有类型的类 3 是 B
$clollege['studentDetails']
array i want to push type from $classType
array based on class what is the type $clollege['studentDetails']
数组我想根据类从$classType
数组中推送类型是什么类型
$clollege['studentDetails']
grade and $classType
class both are same. $clollege['studentDetails']
年级和$classType
类都是相同的。
Expected Output
预期产出
Array
(
[0] => Array
(
[grade] => 2
[hobbies] => Array
(
[0] => A
[1] => B
)
[type] => A
)
[1] => Array
(
[grade] => 2
[hobbies] => Array
(
[0] => A
)
[type] => A
)
[2] => Array
(
[grade] => 3
[hobbies] => Array
(
[0] => C
)
[type] => A
)
I have tried but not working my expected answer,kindly anyone updaye my answer, i have posted my code in below section.我已经尝试过但没有达到我预期的答案,请任何人更新我的答案,我已经在下面的部分中发布了我的代码。 i know this is simple question , if anyone post your so that i can learn from here How to push
我知道这是一个简单的问题,如果有人发布你的,以便我可以从这里学习如何推动
My Code
我的代码
<?php
$classType = [
["class" => "1", "type" => "A"],
["class" => "2", "type" => "A"],
["class" => "3", "type" => "B"]
];
$clollege['studentDetails'] = [
["grade" => "2", "hobbies" => ["A" , "B"] ],
["grade" => "2", "hobbies" => ["A" ] ],
["grade" => "3", "hobbies" => [ "C" ] ]
];
foreach ($classType as $item) {
$clollege['studentDetails'][$item['class']]['type'] = $item['type'];
}
echo "<pre>";
print_r($clollege['studentDetails']);exit;
?>
Update code section
更新代码部分
$studentList['studentDetails'] = [
[ "group" => ["id" => 1 , "grade" => "2"] ],
[ "group" => ["id" => 2 , "grade" => "2", ] ],
[ "group" => [ "id" => 3, "grade" => "3"] ]
];
You can try the following way with the help of array_walk() .您可以在array_walk()的帮助下尝试以下方法。
$classType = [
["class" => "1", "type" => "A"],
["class" => "2", "type" => "A"],
["class" => "3", "type" => "B"]
];
$clollege['studentDetails'] = [
["grade" => "2", "hobbies" => ["A" , "B"] ],
["grade" => "2", "hobbies" => ["A" ] ],
["grade" => "3", "hobbies" => [ "C" ] ]
];
$classType = array_column($classType, 'type', "class");
array_walk($clollege['studentDetails'], function(&$item) use($classType) {
$item['type'] = $classType[$item['grade']];
});
echo '<pre>';
print_r($clollege['studentDetails']);
echo '</pre>';
You can not use grade
as key 2 times in a single or sub array element, i have mentioned solution by using type
index.您不能在单个或子数组元素中使用
grade
作为键 2 次,我已经提到了使用type
索引的解决方案。
You can use foreach
with array_reduce
您可以将
foreach
与array_reduce
一起array_reduce
foreach($clollege['studentDetails'] as $key => &$val){
$grade = $val['grade'];
$val['type'] = array_reduce($classType, function($r, $item) use ($grade){
return ($item['class'] == $grade) ? $item['type'] : $r;
});
}
print_r($clollege);
Updates更新
Change code更改代码
$grade = $val['grade'];
to到
$grade = $val['group']['grade'];
If there is chance for the both of the situation use如果有机会为这两种情况使用
$grade = isset($val['group']['grade']) ? $val['group']['grade'] : $val['grade'];
Working DEMO : https://3v4l.org/5HGLk工作演示: https : //3v4l.org/5HGLk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.