如何在 php 中推送多维数组中的值
how to push values in multidimensional array in php
$classType
数组我正在存储 class 明智的类型,例如 class 1 具有类型 A & class 2 具有类型是 A & class 3类型是 B
$clollege['studentDetails']
数组我想从$classType
数组中推送类型基于class 什么是类型
$clollege['studentDetails']
等级和$classType
class都一样
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
)
我已经尝试但没有得到我预期的答案,请任何人更新我的答案,我在下面的部分 post 编辑了我的代码。我知道这是一个简单的问题,如果有人 post 你这样我可以从这里学习
如何推送
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"] ]
];
您可以在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>';
工作demo。
您不能在单个或子数组元素中使用 grade
作为键 2 次,我已经提到使用 type
索引的解决方案。
您可以将 foreach
与 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);
更新
更改代码
$grade = $val['grade'];
到
$grade = $val['group']['grade'];
如果两种情况都有机会使用
$grade = isset($val['group']['grade']) ? $val['group']['grade'] : $val['grade'];
$classType
数组我正在存储 class 明智的类型,例如 class 1 具有类型 A & class 2 具有类型是 A & class 3类型是 B
$clollege['studentDetails']
数组我想从$classType
数组中推送类型基于class 什么是类型
$clollege['studentDetails']
等级和$classType
class都一样
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
)
我已经尝试但没有得到我预期的答案,请任何人更新我的答案,我在下面的部分 post 编辑了我的代码。我知道这是一个简单的问题,如果有人 post 你这样我可以从这里学习 如何推送
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"] ]
];
您可以在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>';
工作demo。
您不能在单个或子数组元素中使用 grade
作为键 2 次,我已经提到使用 type
索引的解决方案。
您可以将 foreach
与 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);
更新
更改代码
$grade = $val['grade'];
到
$grade = $val['group']['grade'];
如果两种情况都有机会使用
$grade = isset($val['group']['grade']) ? $val['group']['grade'] : $val['grade'];