0

I have an associative array that contains academic subjects data which is :

  • subjectName
  • year
  • semester

here is how it looks:

$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', ),
 );

I want to convert (modify) this array to a 3 dimension array based on the level ($subjects['level']) then the semester ($subjects['semester']).

Eventually i want it to look like this :

  $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'
        )
      )
    )
  );

I've messed around with array_filter() but couldn't achieve anything.

6
  • I was able to filter it based on years using this code: for($y = 1 ; $y < $years_count ; $y ++) { $filtered[] = array_values(array_filter($subjects, function($v) use ($y) { return $v['level'] == "$y"; })); } Commented Oct 8, 2017 at 9:32
  • 1
    An image is worth one thousand words but not when it contains code. Use var_export() to dump the data and use copy & paste to put it in the question as text. Commented Oct 8, 2017 at 10:19
  • 1
    "I want to convert (filter) this array..." -- filtering and conversion are different things. array_filter() does not change the array structure, it just removes some items from the array. It is not the right tool for the job. Commented Oct 8, 2017 at 10:24
  • @axiac thanks for the information bro/sis, Yeah i think that code is more effective to post than images in this case , i was trying to make it simpler to understand (since i always find myself explaining stuff) , But most people who view this are programmers so code is the simplest form xD Commented Oct 8, 2017 at 11:54
  • 1
    If you post code or data in a form that can be easily copy/pasted and used (i.e. as text) you have big chances to get an useful answer. If you post an image that contains the code all you get are down votes. I just retracted mine ;-) Commented Oct 8, 2017 at 12:10

1 Answer 1

1

I am not sure about array_filter to get desired result. But you can use following trick.

$array = [
        ['name'=>'gg','year'=>'1','semster'=>1],
        ['name'=>'gg','year'=>'1','semster'=>2],
        ['name'=>'gg','year'=>'2','semster'=>1]
        ];
$newArray = [];

foreach($array as $arr){
 $newArray[$arr['year']][$arr['semster']][] = $arr;
}
print_r($newArray);

here is the running snippet: https://ideone.com/U2aX8r

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.