Filtering associative array based on two values












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.










share|improve this question

























  • 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"; })); }

    – Mohdule
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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

    – Mohdule
    Oct 8 '17 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 ;-)

    – axiac
    Oct 8 '17 at 12:10
















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.










share|improve this question

























  • 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"; })); }

    – Mohdule
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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

    – Mohdule
    Oct 8 '17 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 ;-)

    – axiac
    Oct 8 '17 at 12:10














0












0








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.










share|improve this question
















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.







php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 2:01







Mohdule

















asked Oct 8 '17 at 9:25









MohduleMohdule

181311




181311













  • 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"; })); }

    – Mohdule
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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

    – Mohdule
    Oct 8 '17 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 ;-)

    – axiac
    Oct 8 '17 at 12:10



















  • 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"; })); }

    – Mohdule
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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.

    – axiac
    Oct 8 '17 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

    – Mohdule
    Oct 8 '17 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 ;-)

    – axiac
    Oct 8 '17 at 12:10

















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"; })); }

– Mohdule
Oct 8 '17 at 9:32







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"; })); }

– Mohdule
Oct 8 '17 at 9:32






1




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.

– axiac
Oct 8 '17 at 10:19





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.

– axiac
Oct 8 '17 at 10:19




1




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.

– axiac
Oct 8 '17 at 10:24





"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.

– axiac
Oct 8 '17 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

– Mohdule
Oct 8 '17 at 11:54







@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

– Mohdule
Oct 8 '17 at 11:54






1




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

– axiac
Oct 8 '17 at 12:10





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

– axiac
Oct 8 '17 at 12:10












1 Answer
1






active

oldest

votes


















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






share|improve this answer
























  • Thank you very much :D!

    – Mohdule
    Oct 8 '17 at 11:52











  • Always welcome !!☺

    – Ankur Garg
    Oct 8 '17 at 12:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46629532%2ffiltering-associative-array-based-on-two-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer
























  • Thank you very much :D!

    – Mohdule
    Oct 8 '17 at 11:52











  • Always welcome !!☺

    – Ankur Garg
    Oct 8 '17 at 12:24
















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






share|improve this answer
























  • Thank you very much :D!

    – Mohdule
    Oct 8 '17 at 11:52











  • Always welcome !!☺

    – Ankur Garg
    Oct 8 '17 at 12:24














1












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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 8 '17 at 9:54









Ankur GargAnkur Garg

43628




43628













  • Thank you very much :D!

    – Mohdule
    Oct 8 '17 at 11:52











  • Always welcome !!☺

    – Ankur Garg
    Oct 8 '17 at 12:24



















  • Thank you very much :D!

    – Mohdule
    Oct 8 '17 at 11:52











  • Always welcome !!☺

    – Ankur Garg
    Oct 8 '17 at 12:24

















Thank you very much :D!

– Mohdule
Oct 8 '17 at 11:52





Thank you very much :D!

– Mohdule
Oct 8 '17 at 11:52













Always welcome !!☺

– Ankur Garg
Oct 8 '17 at 12:24





Always welcome !!☺

– Ankur Garg
Oct 8 '17 at 12:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46629532%2ffiltering-associative-array-based-on-two-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas