回答編集履歴
4
補足
test
CHANGED
@@ -143,3 +143,55 @@
|
|
143
143
|
|
144
144
|
|
145
145
|
を参考にソートして、ソート後の配列を必要な数だけ取り出す という感じで実装できるかと思います。
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
データの持ち方を変えればもっと楽な例
|
152
|
+
|
153
|
+
---
|
154
|
+
|
155
|
+
```PHP
|
156
|
+
|
157
|
+
<?php
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
//元のデータがこんな感じであるとして
|
162
|
+
|
163
|
+
$arr = [
|
164
|
+
|
165
|
+
['val'=>'a1','date'=>'2020-02-25 01:00:00'],
|
166
|
+
|
167
|
+
['val'=>'a2','date'=>'2020-02-25 02:00:00'],
|
168
|
+
|
169
|
+
['val'=>'a3','date'=>'2020-02-25 03:00:00']
|
170
|
+
|
171
|
+
];
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
foreach($arr as $row){
|
176
|
+
|
177
|
+
$newArr[$row['val']] = $row;
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
$new = ['val'=>'a4','date'=>'2020-02-25 04:00:00'];
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
//データが無いときは追加であるときは更新
|
188
|
+
|
189
|
+
$newArr[$new['val']] = $new;
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
var_dump($newArr);
|
196
|
+
|
197
|
+
```
|
3
追記
test
CHANGED
@@ -26,7 +26,11 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
-
$new = ['val'=>'a
|
29
|
+
$new = ['val'=>'a4','date'=>'2020-02-25 04:00:00'];
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
$isInArray = false;
|
30
34
|
|
31
35
|
foreach($arr as $key => $row ){
|
32
36
|
|
@@ -34,7 +38,17 @@
|
|
34
38
|
|
35
39
|
$arr[$key]['date'] = $new['date'];
|
36
40
|
|
41
|
+
$isInArray = true;
|
42
|
+
|
37
43
|
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
if($isInArray === false){
|
50
|
+
|
51
|
+
$arr[] = $new;
|
38
52
|
|
39
53
|
}
|
40
54
|
|
@@ -44,9 +58,11 @@
|
|
44
58
|
|
45
59
|
|
46
60
|
|
61
|
+
|
62
|
+
|
47
63
|
/*
|
48
64
|
|
49
|
-
array(
|
65
|
+
array(4) {
|
50
66
|
|
51
67
|
[0] =>
|
52
68
|
|
@@ -72,7 +88,7 @@
|
|
72
88
|
|
73
89
|
'date' =>
|
74
90
|
|
75
|
-
string(19) "2020-02-25 0
|
91
|
+
string(19) "2020-02-25 02:00:00"
|
76
92
|
|
77
93
|
}
|
78
94
|
|
@@ -87,6 +103,20 @@
|
|
87
103
|
'date' =>
|
88
104
|
|
89
105
|
string(19) "2020-02-25 03:00:00"
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
[3] =>
|
110
|
+
|
111
|
+
array(2) {
|
112
|
+
|
113
|
+
'val' =>
|
114
|
+
|
115
|
+
string(2) "a4"
|
116
|
+
|
117
|
+
'date' =>
|
118
|
+
|
119
|
+
string(19) "2020-02-25 04:00:00"
|
90
120
|
|
91
121
|
}
|
92
122
|
|
2
エラー修正
test
CHANGED
@@ -9,6 +9,8 @@
|
|
9
9
|
|
10
10
|
|
11
11
|
```PHP
|
12
|
+
|
13
|
+
|
12
14
|
|
13
15
|
|
14
16
|
|
@@ -24,19 +26,13 @@
|
|
24
26
|
|
25
27
|
|
26
28
|
|
27
|
-
$new = ['val'=>'a2','date'=>'2020-02-25 04:00:00'];
|
29
|
+
$new = ['val'=>'a2','date'=>'2020-02-25 04:00:00'];
|
28
30
|
|
31
|
+
foreach($arr as $key => $row ){
|
29
32
|
|
33
|
+
if($row['val'] === $new['val']){
|
30
34
|
|
31
|
-
foreach($new as $newRow){
|
32
|
-
|
33
|
-
foreach($arr as $key => $row ){
|
34
|
-
|
35
|
-
if($row['val'] === $newRow['val']){
|
36
|
-
|
37
|
-
|
35
|
+
$arr[$key]['date'] = $new['date'];
|
38
|
-
|
39
|
-
}
|
40
36
|
|
41
37
|
}
|
42
38
|
|
@@ -76,7 +72,7 @@
|
|
76
72
|
|
77
73
|
'date' =>
|
78
74
|
|
79
|
-
string(19) "2020-02-25 0
|
75
|
+
string(19) "2020-02-25 04:00:00"
|
80
76
|
|
81
77
|
}
|
82
78
|
|
@@ -95,8 +91,6 @@
|
|
95
91
|
}
|
96
92
|
|
97
93
|
}
|
98
|
-
|
99
|
-
|
100
94
|
|
101
95
|
*/
|
102
96
|
|
1
補足
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
冗長な感じはありますが、愚直にやるならこうかなあ。
|
6
6
|
|
7
|
-
ポイントとしては`$arr`を直接いじりたいなら`$arr[$key][
|
7
|
+
ポイントとしては`$arr`を直接いじりたいなら`$arr[$key]['date']`みたいにする必要がある点です。(これを`$row['date'] = $newRow['date']`としても`$arr`は書き換わりません。)
|
8
8
|
|
9
9
|
|
10
10
|
|