【问题标题】:how to push values in multidimensional array in php如何在php中推送多维数组中的值
【发布时间】:2019-12-07 11:45:51
【问题描述】:

$clollege['studentDetails'] 数组我想根据类从$classType 数组中推送类型是什么类型

$clollege['studentDetails'] 等级和$classType 等级相同。

预期输出

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

        )

我已经尝试但没有达到预期的答案,请任何人更新我的答案,我已在下面的部分中发布了我的代码。我知道这是一个简单的问题,如果有人发布你的,以便我可以从这里学习 怎么推

我的代码

<?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;
?>

更新代码部分

$studentList['studentDetails'] = [
    [ "group" => ["id" => 1 , "grade" => "2"] ],
    [ "group" => ["id" => 2 , "grade" => "2", ] ],
    [ "group" => [ "id" => 3, "grade" => "3"] ]
];

【问题讨论】:

  • 什么是类,它对预期输出有何影响?是否总是将索引 0 与索引 0 混合,等等?
  • grade 你不能在数组中使用相同的键名
  • 你能请任何人发布你的答案吗?我试过但我无法得到我预期的答案
  • 你为什么期望有两个相同的密钥grade

标签: php php-7


【解决方案1】:

您可以在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

【讨论】:

  • 其为echo。已更新,您可以立即试用。
  • 一个小问题假设$studentList['studentDetails']数组等级它在组数组下,如何更改你的代码,预期的输出是一样的。
  • 我已经在更新代码部分发布了我的 $studentList['studentDetails']
  • @KaniR,我不清楚你的问题。什么是新的数据结构?是$clollege['studentDetails'] 还是$studentList['studentDetails']?很混乱。清楚地描述这两种数据结构是什么。
  • 我现在得到答案了
【解决方案2】:

您不能在单个或子数组元素中使用 grade 作为键 2 次,我已经提到了使用 type 索引的解决方案。

您可以将foreacharray_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'];

工作演示:https://3v4l.org/5HGLk

【讨论】:

  • 一个小问题假设$studentList['studentDetails']数组grade它在组数组下,如何改变你的代码,预期的输出是一样的。
  • 我已经在更新代码部分发布了我的 $studentList['studentDetails']
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
相关资源
最近更新 更多