回答編集履歴

3

処理速度に関する回答を追記

2017/02/10 06:28

投稿

motuo
motuo

スコア3027

test CHANGED
@@ -138,7 +138,9 @@
138
138
 
139
139
 
140
140
 
141
- 後は検索のfunctionでこの様な書き方もできますね
141
+ 後は検索のfunctionでこの様な書き方もできますね
142
+
143
+ でも、行数は短くなりますが、Array_searchは遅いので、上記の方法でループした方が処理は早いかもしれません。
142
144
 
143
145
  ```php
144
146
 

2

\$idの判定分を修正

2017/02/10 06:27

投稿

motuo
motuo

スコア3027

test CHANGED
@@ -146,7 +146,7 @@
146
146
 
147
147
  $id = array_search($searchKey, array_column($searchArray, 'id'));
148
148
 
149
- if($id){
149
+ if($id !== false){
150
150
 
151
151
  return $searchArray[$id];
152
152
 

1

別解を追記

2017/02/10 06:25

投稿

motuo
motuo

スコア3027

test CHANGED
@@ -135,3 +135,27 @@
135
135
  }
136
136
 
137
137
  ```
138
+
139
+
140
+
141
+ 後は検索のfunctionでこの様な書き方もできますね
142
+
143
+ ```php
144
+
145
+ function key_search2($searchKey,$searchArray){
146
+
147
+ $id = array_search($searchKey, array_column($searchArray, 'id'));
148
+
149
+ if($id){
150
+
151
+ return $searchArray[$id];
152
+
153
+ }else{
154
+
155
+ return FALSE;
156
+
157
+ }
158
+
159
+ }
160
+
161
+ ```