Traverse JSON data to group the same attribute data?

Source: Internet
Author: User
{"Module_list": [{"Content": {"Module-type": "List-1", "Module": "L Ist "," type ":" 1 "," Name ":" Job List "," Logo1 ":" Imgurl1 "," title 1 ":" Baidu1 "," DESC1 ":" Hahahha1 "," URL1 ":" Www.baidu.com1 "," Logo2 ":" Imgurl                2 "," Title2 ":" Baidu2 "," DESC2 ":" Hahahha2 "," Url2 ":" Www.baidu.com2 ",  "Logo3": "Imgurl3", "Title3": "Baidu3", "DESC3": "Hahahha3", "Url3": "Www.baidu.com3", "Logo4": "", "Title4": "", "Desc4": "", "ur L4 ":", "Jobs": [{"Name": "Product Manager", "Sala                        Ry ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",      "Mark": "Group:1"              }, {"Name": "Product manager", "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group:2 "}, {" Name ":" Product manager "," Salary ":" 10k- 15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Group:3 "}, {" Name ":" Product manager "," salary " : "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "M Ark ":" Group:4 "}, {" Name ":" Product manager ","                        Salary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",  "Mark": "Group:1"                  }, {"Name": "Product manager", "Salary": "10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Gro Up:1 "}, {" Name ":" Product manager "," Salary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Group:4 "}, {" Name ":" Product Manager "," Sal                        ary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",                        "Mark": "Group:4"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group:3 "}, {" Name ":" Product manager "," Salary ":" 10k -15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ": "Group:2"}, {"Name": "Product manager", "salary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Group:2 "}]},}]}

JSON stores some job information, how to group the information according to Mark's value, for example, mark value for group:1 into a group, mark value of Group:2 in a group, and then in the front of the different modules. How do I traverse the data? How do I put a different array of different mark values after the traversal?

public static function BuildList ($module) {$jobList 2 = ""; if ($module [' Jobs ']) {foreach ($module [' Jobs '] as $job) {$jobList 2. = <<{$job [' name ']}{$job [' salary ']}                html }} return <<                    
  • {$module [' Title1 ']}

    {$module [' DESC1 ']}

      {$jobList 2}

    More jobs

  • {$module [' Title2 ']}

    {$module [' DESC2 ']}

      {$jobList 2}

    More jobs

  • {$module [' Title3 ']}

    {$module [' desc3 ']}

      {$jobList 2}

    More jobs

  • {$module [' title4 ']}

    {$module [' DESC4 ']}

      {$jobList 2}

    More jobs

  • html }

    is to make the data appear below the corresponding module according to the values of the different group

    Reply content:

    {"Module_list": [{"Content": {"Module-type": "List-1", "Module": "L Ist "," type ":" 1 "," Name ":" Job List "," Logo1 ":" Imgurl1 "," title 1 ":" Baidu1 "," DESC1 ":" Hahahha1 "," URL1 ":" Www.baidu.com1 "," Logo2 ":" Imgurl                2 "," Title2 ":" Baidu2 "," DESC2 ":" Hahahha2 "," Url2 ":" Www.baidu.com2 ",  "Logo3": "Imgurl3", "Title3": "Baidu3", "DESC3": "Hahahha3", "Url3": "Www.baidu.com3", "Logo4": "", "Title4": "", "Desc4": "", "ur L4 ":", "Jobs": [{"Name": "Product Manager", "Sala                        Ry ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",      "Mark": "Group:1"              }, {"Name": "Product manager", "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group:2 "}, {" Name ":" Product manager "," Salary ":" 10k- 15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Group:3 "}, {" Name ":" Product manager "," salary " : "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "M Ark ":" Group:4 "}, {" Name ":" Product manager ","                        Salary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",  "Mark": "Group:1"                  }, {"Name": "Product manager", "Salary": "10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Gro Up:1 "}, {" Name ":" Product manager "," Salary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Group:4 "}, {" Name ":" Product Manager "," Sal                        ary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",                        "Mark": "Group:4"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group:3 "}, {" Name ":" Product manager "," Salary ":" 10k -15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ": "Group:2"}, {"Name": "Product manager", "salary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ":" Group:2 "}]},}]}

    JSON stores some job information, how to group the information according to Mark's value, for example, mark value for group:1 into a group, mark value of Group:2 in a group, and then in the front of the different modules. How do I traverse the data? How do I put a different array of different mark values after the traversal?

    public static function BuildList ($module) {$jobList 2 = ""; if ($module [' Jobs ']) {foreach ($module [' Jobs '] as $job) {$jobList 2. = <<{$job [' name ']}{$job [' salary ']}                html }} return <<                    
  • {$module [' Title1 ']}

    {$module [' DESC1 ']}

      {$jobList 2}

    More jobs

  • {$module [' Title2 ']}

    {$module [' DESC2 ']}

      {$jobList 2}

    More jobs

  • {$module [' Title3 ']}

    {$module [' desc3 ']}

      {$jobList 2}

    More jobs

  • {$module [' title4 ']}

    {$module [' DESC4 ']}

      {$jobList 2}

    More jobs

  • html }

    is to make the data appear below the corresponding module according to the values of the different group

    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;
    So the map returned is a key,value
    {
    GROUP1:[JOB1,JOB2],
    GROUP2:[JOB3,JOB4]
    }
    It should be what you want.

    Loop 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 would be like this:

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

    php$json = ' {"Module_list": [{"Content": {"Module-type": "List-1", "Mo                Dule ":" 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": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group: 1 "}, {" Name ":" Product manager "," Salary ":" 10  k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou "," Mark ": "Group:2"}, {"Name": "Product Manager", "Salar                        Y ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",                        "Mark": "Group:3"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou",                        "Mark": "Group:4"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "GRoup:1 "}, {" Name ":" Product manager "," salary ": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Ma RK ":" Group:1 "}, {" Name ":" Product Manager "," s                        Alary ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",                        "Mark": "Group:4"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou",                        "Mark": "Group:4"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group:3"}, {"Name": "Product Manager", "Sala                        Ry ":" 10k-15k "," url ":" Www.baidu.com "," desc ":" Guangzhou ",                        "Mark": "Group:2"}, {"Name": "Product Manager",                        "Salary": "10k-15k", "url": "Www.baidu.com", "desc": "Guangzhou", "Mark": "Group:2"}]}]} '; $job = Json_decode ($json, True $arr = [];foreach ($job [' module_list '][0][' content '] [' Jobs '] as $key + $value) {$mark = substr ($value [' Mark '],-  );  $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.