根据两个值过滤关联数组

时间:2017-10-08 09:25:42

标签: php arrays

我有一个关联数组,其中包含学术科目数据:

  • 主旨名称
  • 学期

这是它的外观:

$subjects = array (
    0 => array (
        'subjectName' => 'Introduction to Programming', 
        'year' => '1',
        'semester' => '1', ),
    1 => array (
        'subjectName' => 'Introduction into Computer Science',
        'year' => '1',
        'semester' => '1', ),
    2 => array (
        'subjectName' => 'Computer Architecture',
        'year' => '1',
        'semester' => '2', ),
    3 => array (
        'subjectName' => 'Statistics',
        'year' => '2',
        'semester' => '1', ),
    4 => array (
        'subjectName' => 'DBMS',
        'year' => '2',
        'semester' => '1', ),
 );

我想根据级别($subjects['level'])然后是学期($subjects['semester'])将此数组转换(修改)为3维数组。

最终我希望它看起来像这样:

  $newArray = array(
    0 => array(
      0 => array(
        0 => array(
          'subjectName' => 'Introduction to Programming',
          'year' => '1',
          'semester' => '1'
        ),
        1 => array(
          'subjectName' => 'Introduction into Computer Science',
          'year' => '1',
          'semester' => '1'
        )
      ),
      1 => array(
        0 => array(
          'subjectName' => 'Computer Architecture',
          'year' => '1',
          'semester' => '2'
        )
      )
    ),
    1 => array(
      0 => array(
        0 => array(
          'subjectName' => 'DBMS',
          'year' => '2',
          'semester' => '1'
        )
      )
    )
  );

我与array_filter()混淆了但是无法实现任何目标。

1 个答案:

答案 0 :(得分:1)

我不确定stream= File.stream!(path) Agent.start(fn -> %{"stream" => stream} end, name: {:global, :my_stream} ) 是否能获得理想的结果。但你可以使用以下技巧。

stream= Agent.get( {:global, :my_stream}, fn %{"stream" => stream } -> stream end )
Task.start_link(Stream, :run, [stream])

这是正在运行的代码段:https://ideone.com/U2aX8r