PHP
1$color=filter_input(INPUT_POST,"color");
2$data = [
3 "test"=>[
4 [
5 "color"=>"red",
6 "type"=>"a",
7 ],
8 [
9 "color"=>"red",
10 "type"=>"b",
11 ],
12 [
13 "color"=>"blue",
14 "type"=>"c",
15 ],
16 ],
17];
18
19$result = array_filter($data["test"],function($x) use($color) {
20 return $x["color"] === $color;
21});
22print_r($result);
追加分
javascript
1<script>
2$(function(){
3 $('.result').html('aaa');
4 $('button').on('click',function(event) {
5 event.preventDefault();
6 var html = '';
7 var formdata = new FormData($('#form').get(0));
8 $.ajax({
9 "url": 'ajax.php',
10 "type": "POST",
11 "dataType": "html",
12 "data":formdata,
13 "cache":false,
14 "processData": false,
15 "contentType": false,
16 }).done(function(data){
17 console.log(data);
18 $('.result').html(data);
19 });
20 });
21});
22</script>
23<form id="form" name="search" action="" method="POST">
24color:
25<label><input type="radio" name="color" value="red" checked>赤</label>
26<label><input type="radio" name="color" value="blue">青</label><br>
27type:
28<label><input type="radio" name="type" value="a" checked>a</label>
29<label><input type="radio" name="type" value="b">b</label>
30<label><input type="radio" name="type" value="c">c</label><br>
31<button class="submit" value="検索">検索</button>
32</form>
33<section class="result"></section>
PHP
1$color=filter_input(INPUT_POST,"color");
2$type=filter_input(INPUT_POST,"type");
3$data = [
4 "test"=>[
5 [
6 "color"=>"red",
7 "type"=>"a",
8 ],
9 [
10 "color"=>"red",
11 "type"=>"b",
12 ],
13 [
14 "color"=>"blue",
15 "type"=>"c",
16 ],
17 ],
18];
19
20$result = array_filter($data["test"],function($x) use($color,$type) {
21 return $x["color"]==$color and $x["type"]==$type;
22});
23print_r($result);
24?>
さらに追記
何も選択されていなければ全て表示。
そこから選択されたものを以外を削っていくような形
HTML
1<script>
2$(function(){
3 $('.result').html('aaa');
4 $('button').on('click',function(event) {
5 event.preventDefault();
6 var html = '';
7 var formdata = new FormData($('#form').get(0));
8 $.ajax({
9 "url": 'ajax.php',
10 "type": "POST",
11 "dataType": "html",
12 "data":formdata,
13 "cache":false,
14 "processData": false,
15 "contentType": false,
16 }).done(function(data){
17 console.log(data);
18 $('.result').html(data);
19 });
20 });
21});
22</script>
23<form id="form" name="search" action="" method="POST">
24color:
25<label><input type="radio" name="color" value="red">赤</label>
26<label><input type="radio" name="color" value="blue">青</label><br>
27type:
28<label><input type="radio" name="type" value="a">a</label>
29<label><input type="radio" name="type" value="b">b</label>
30<label><input type="radio" name="type" value="c">c</label><br>
31<button class="submit" value="検索">検索</button>
32<input type="reset" value="リセット">
33</form>
34<section class="result"></section>
少しパターンを増やしました
PHP
1$color=filter_input(INPUT_POST,"color");
2$type=filter_input(INPUT_POST,"type");
3$data = [
4 "test"=>[
5 [
6 "color"=>"red",
7 "type"=>"a",
8 ],
9 [
10 "color"=>"red",
11 "type"=>"b",
12 ],
13 [
14 "color"=>"blue",
15 "type"=>"a",
16 ],
17 [
18 "color"=>"blue",
19 "type"=>"c",
20 ],
21 [
22 "color"=>"yellow",
23 "type"=>"a",
24 ],
25 ],
26];
27
28$result = array_filter($data["test"],function($x) use($color,$type) {
29 return (is_null($color) or $x["color"]==$color) and (is_null($type) or $x["type"]==$type);
30});
31print_r($result);
$colorと$typeはand検索($colorも$typeも両方ヒットするもの)にしてあります
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/02 03:07
2018/08/02 03:27
2018/08/02 03:27
2018/08/02 03:32
2018/08/02 03:34
2018/08/02 03:40
2018/08/02 08:16
2018/08/02 09:38