Traverse json data and group the same attribute data?

Source: Internet
Author: User
{Code ...} json stores some job information. How can I group this information based on the mark value? For example, if the mark value is group: 1, put it in a group. The mark value is group: 2 is placed in a group, and then displayed in different modules on the front-end. How to traverse data ?...
{"Module_list": [{"content": {"module-type": "list-1", "module": "list", "type": "1 ", "name": "job list", "logo1": "imgurl1", "title1": "baidu1", "desc1": "hahahha1", "url1": "www. baidu. com1 "," logo2 ":" imgurl2 "," title2 ":" baidu2 "," desc2 ":" hahahha2 "," url2 ":" www. baidu. com2 "," logo3 ":" imgurl3 "," title3 ":" baidu3 "," desc3 ":" hahahha3 "," url3 ":" www. baidu. com3 "," logo4 ":" "," title4 ":" "," desc4 ":" "," url4 ":" "," jobs ": [{"name": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou ", "mark": "group: 1" },{ "name": "Product Manager", "salary": "10 k-15 k", "url ": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2" },{ "name": "Product Manager", "salary ": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 3" },{ "name ": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 4 "},{" name ":" Product Manager "," salary ":" 10 k-15 k "," url ":" www.baidu.com "," desc ": "Guangzhou", "mark": "group: 1" },{ "name": "Product Manager", "salary": "10 k-15 k ", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 1" },{ "name": "Product Manager ", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 4 "}, {"name": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou ", "mark": "group: 4" },{ "name": "Product Manager", "salary": "10 k-15 k", "url ": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 3" },{ "name": "Product Manager", "salary ": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2" },{ "name ": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2 "}]},}]}

Json stores some job information. How can I group this information based on the mark value? For example, if the mark value is group: 1, put it in a group. The mark value is group: 2 is placed in a group, and then displayed in different modules on the front-end. How to traverse data? After traversing, how can we put different mark values into different arrays?

Public static function buildList ($ module) {$ jobList2 = ""; if ($ module ['job']) {foreach ($ module ['job'] as $ job) {$ jobList2. = <{$ Job ['name']} {$ job ['salary ']}Html ;}} return <            

  • {$ Module ['title1']}

    {$ Module ['sc1']}

      {$ JobList2}

    More positions

  • {$ Module ['title2']}

    {$ Module ['sc2']}

      {$ JobList2}

    More positions

  • {$ Module ['title3']}

    {$ Module ['sc3']}

      {$ JobList2}

    More positions

  • {$ Module ['title4']}

    {$ Module ['sc4']}

      {$ JobList2}

    More positions

Html ;}

The data is displayed under the corresponding module based on the values of different groups.

Reply content:
{"Module_list": [{"content": {"module-type": "list-1", "module": "list", "type": "1 ", "name": "job list", "logo1": "imgurl1", "title1": "baidu1", "desc1": "hahahha1", "url1": "www. baidu. com1 "," logo2 ":" imgurl2 "," title2 ":" baidu2 "," desc2 ":" hahahha2 "," url2 ":" www. baidu. com2 "," logo3 ":" imgurl3 "," title3 ":" baidu3 "," desc3 ":" hahahha3 "," url3 ":" www. baidu. com3 "," logo4 ":" "," title4 ":" "," desc4 ":" "," url4 ":" "," jobs ": [{"name": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou ", "mark": "group: 1" },{ "name": "Product Manager", "salary": "10 k-15 k", "url ": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2" },{ "name": "Product Manager", "salary ": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 3" },{ "name ": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 4 "},{" name ":" Product Manager "," salary ":" 10 k-15 k "," url ":" www.baidu.com "," desc ": "Guangzhou", "mark": "group: 1" },{ "name": "Product Manager", "salary": "10 k-15 k ", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 1" },{ "name": "Product Manager ", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 4 "}, {"name": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou ", "mark": "group: 4" },{ "name": "Product Manager", "salary": "10 k-15 k", "url ": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 3" },{ "name": "Product Manager", "salary ": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2" },{ "name ": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2 "}]},}]}

Json stores some job information. How can I group this information based on the mark value? For example, if the mark value is group: 1, put it in a group. The mark value is group: 2 is placed in a group, and then displayed in different modules on the front-end. How to traverse data? After traversing, how can we put different mark values into different arrays?

Public static function buildList ($ module) {$ jobList2 = ""; if ($ module ['job']) {foreach ($ module ['job'] as $ job) {$ jobList2. = <{$ Job ['name']} {$ job ['salary ']}Html ;}} return <            

  • {$ Module ['title1']}

    {$ Module ['sc1']}

      {$ JobList2}

    More positions

  • {$ Module ['title2']}

    {$ Module ['sc2']}

      {$ JobList2}

    More positions

  • {$ Module ['title3']}

    {$ Module ['sc3']}

      {$ JobList2}

    More positions

  • {$ Module ['title4']}

    {$ Module ['sc4']}

      {$ JobList2}

    More positions

Html ;}

The data is displayed under the corresponding module based on the values of different groups.

Var map = {};
For (var I = 0, l = jobs. length; I Var key = jobs [I] ['mark'];
Map [key] = map [key] | (map [key] = []);
Map [key]. push (jobs [I]);
}
Return map;
The returned map is a key and value.
{
Group1: [job1, job2],
Group2: [job3, job4]
}
It should be what you want

Cyclic jobs array:

var jobsSortObject = {}; for(var i =0; i< jobs.length; i++){    var job = jobs[i],        mark = job.mark,        jobItem = jobsSortObject[mark];    if(jobItem){        jobsSortObject[mark].push(job);    }else{        jobsSortObject[mark] = [job];    }}

JobsSortObject will look like this:

{    'group:1' : [{} , {}, {}],    'group:2' : [ {} ]}

php$ Json = '{"module_list": [{"content": {"module-type": "list-1", "module": "list", "type ": "1", "name": "job list", "logo1": "imgurl1", "title1": "baidu1", "desc1": "hahahha1 ", "url1": "www. baidu. com1 "," logo2 ":" imgurl2 "," title2 ":" baidu2 "," desc2 ":" hahahha2 "," url2 ":" www. baidu. com2 "," logo3 ":" imgurl3 "," title3 ":" baidu3 "," desc3 ":" hahahha3 "," url3 ":" www. baidu. com3 "," logo4 ":" "," title4 ":" "," desc4 ":" "," url4 ":" "," jobs ": [{"name": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou ", "mark": "group: 1" },{ "name": "Product Manager", "salary": "10 k-15 k", "url ": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2" },{ "name": "Product Manager", "salary ": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 3" },{ "name ": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 4 "},{" name ":" Product Manager "," salary ":" 10 k-15 k "," url ":" www.baidu.com "," desc ": "Guangzhou", "mark": "group: 1" },{ "name": "Product Manager", "salary": "10 k-15 k ", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 1" },{ "name": "Product Manager ", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 4 "}, {"name": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou ", "mark": "group: 4" },{ "name": "Product Manager", "salary": "10 k-15 k", "url ": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 3" },{ "name": "Product Manager", "salary ": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2" },{ "name ": "Product Manager", "salary": "10 k-15 k", "url": "www.baidu.com", "desc": "Guangzhou", "mark": "group: 2 "}]} '; $ job = json_decode ($ json, true); $ arr = []; foreach ($ job ['module _ list'] [0] ['content'] ['job'] as $ key => $ value) {$ mark = substr ($ value ['mark'],-1,1); $ arr [$ value ['mark'] [] = $ value; $ arr [$ value ['mark'] ['title '. $ mark] = $ job ['module _ list'] [0] ['content'] ['title '. $ mark]; $ arr [$ value ['mark'] ['desc '. $ mark] = $ job ['module _ list'] [0] ['content'] ['desc '. $ mark];} print_r ($ arr );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.