遍历json数据,对同一属性数据进行分组?
<code>{ "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" } ] }, } ] } </code>
json存储了一些职位信息,怎么根据mark的值,对这些信息进行分组 ,例如mark值为group:1的放到一个分组里,,mark值为group:2的放到一个分组里,然后在前端显示在不同模块里。该怎么对数据进行遍历?遍历之后怎么根据不同的mark值放到不同的数组里呢
<code> public static function buildList($module){ $jobList2 = ""; if($module['jobs']){ foreach($module['jobs'] as $job){ $jobList2 .= <a class="post post2">{$job['name']}</a> <span class="salary">{$job['salary']}</span> html; } } return <div class="content"> <ul class="cmpList"> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title1']}</h1> <p>{$module['desc1']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title2']}</h1> <p>{$module['desc2']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title3']}</h1> <p>{$module['desc3']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title4']}</h1> <p>{$module['desc4']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> </ul> </div> html; } </code>
就是根据不同的group的值,让数据显示在相应的模块下面
回复内容:
<code>{ "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" } ] }, } ] } </code>
json存储了一些职位信息,怎么根据mark的值,对这些信息进行分组 ,例如mark值为group:1的放到一个分组里,,mark值为group:2的放到一个分组里,然后在前端显示在不同模块里。该怎么对数据进行遍历?遍历之后怎么根据不同的mark值放到不同的数组里呢
<code> public static function buildList($module){ $jobList2 = ""; if($module['jobs']){ foreach($module['jobs'] as $job){ $jobList2 .= <a class="post post2">{$job['name']}</a> <span class="salary">{$job['salary']}</span> html; } } return <div class="content"> <ul class="cmpList"> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title1']}</h1> <p>{$module['desc1']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title2']}</h1> <p>{$module['desc2']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title3']}</h1> <p>{$module['desc3']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> <li class="cmpList-li"> <img src="" alt=""> <h1 id="module-title">{$module['title4']}</h1> <p>{$module['desc4']}</p> <ul class="jobList2"> {$jobList2} </ul> <p class="more">更多职位</p> </li> </ul> </div> html; } </code>
就是根据不同的group的值,让数据显示在相应的模块下面
var map = {};
for(var i=0,l=jobs.length;i
map[key] = map[key] || (map[key] = []);
map[key].push(jobs[i]);
}
return map;
这样返回的map就是一个key,value
{
group1:[job1,job2],
group2:[job3,job4]
}
应该就是你想要的
循环jobs数组:
<code>var jobsSortObject = {}; for(var i =0; i</code>
jobsSortObject就会是这个样子:
<code>{ 'group:1' : [{} , {}, {}], 'group:2' : [ {} ] } </code>
<code>php</code><code>$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); </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
