遍曆json資料,對同一屬性資料進行分組?

來源:互聯網
上載者:User
{    "module_list": [        {            "content": {                "module-type": "list-1",                "module": "list",                "type": "1",                "name": "職位列表 ",                "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": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:3"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:3"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    }                ]            },        }    ]}

json儲存了一些職位資訊,怎麼根據mark的值,對這些資訊進行分組 ,例如mark值為group:1的放到一個分組裡,,mark值為group:2的放到一個分組裡,然後在最上層顯示在不同模組裡。該怎麼對資料進行遍曆?遍曆之後怎麼根據不同的mark值放到不同的數組裡呢

    public static function buildList($module){        $jobList2 = "";        if($module['jobs']){            foreach($module['jobs'] as $job){                $jobList2 .= <<                    {$job['name']}                    {$job['salary']}                html;            }        }        return <<                    
  • {$module['title1']}

    {$module['desc1']}

      {$jobList2}

    更多職位

  • {$module['title2']}

    {$module['desc2']}

      {$jobList2}

    更多職位

  • {$module['title3']}

    {$module['desc3']}

      {$jobList2}

    更多職位

  • {$module['title4']}

    {$module['desc4']}

      {$jobList2}

    更多職位

  • html; }

    就是根據不同的group的值,讓資料顯示在相應的模組下面

    回複內容:

    {    "module_list": [        {            "content": {                "module-type": "list-1",                "module": "list",                "type": "1",                "name": "職位列表 ",                "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": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:3"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:3"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    }                ]            },        }    ]}

    json儲存了一些職位資訊,怎麼根據mark的值,對這些資訊進行分組 ,例如mark值為group:1的放到一個分組裡,,mark值為group:2的放到一個分組裡,然後在最上層顯示在不同模組裡。該怎麼對資料進行遍曆?遍曆之後怎麼根據不同的mark值放到不同的數組裡呢

        public static function buildList($module){        $jobList2 = "";        if($module['jobs']){            foreach($module['jobs'] as $job){                $jobList2 .= <<                    {$job['name']}                    {$job['salary']}                html;            }        }        return <<                    
  • {$module['title1']}

    {$module['desc1']}

      {$jobList2}

    更多職位

  • {$module['title2']}

    {$module['desc2']}

      {$jobList2}

    更多職位

  • {$module['title3']}

    {$module['desc3']}

      {$jobList2}

    更多職位

  • {$module['title4']}

    {$module['desc4']}

      {$jobList2}

    更多職位

  • html; }

    就是根據不同的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;
    這樣返回的map就是一個key,value
    {
    group1:[job1,job2],
    group2:[job3,job4]
    }
    應該就是你想要的

    迴圈jobs數組:

    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就會是這個樣子:

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

    php$json = '{    "module_list": [        {            "content": {                "module-type": "list-1",                "module": "list",                "type": "1",                "name": "職位列表 ",                "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": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:3"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:1"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:4"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:3"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    },                    {                        "name": "產品經理",                        "salary": "10k-15k",                        "url": "www.baidu.com",                        "desc": "廣州",                        "mark": "group:2"                    }                ]            }        }    ]}';$job = json_decode($json,true);$arr  = [];foreach ($job['module_list'][0]['content']['jobs'] 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);
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.