Method for ThinkPHP Query to Return a Simple Field Array

  • 2021-07-16 02:05:38
  • OfStack

This paper describes the method of ThinkPHP query to return simple field array, which is a very practical function in ThinkPHP program design. The specific methods are as follows:

Generally speaking, select statement is used. All the fields returned are complex field arrays. For example, the following is a simple query:


$map['parentid'] = $id;
$sub_ids = D('Category')->where($map)->field("catid")->select();

After the query, the result is:


[{"catid":"23"},{"catid":"24"},{"catid":"25"},{"catid":"26"},{"catid":"27"},{"catid":"28"},{"catid":"29"},{"catid":"30"}]

It can be seen from the structure that this is a complex array, and its element is an map.
If we just need a simple array containing only field elements, we can use the following methods:


$sub_ids = D('Category')->where($map)->getField('catid',true);

After the query, the result is:


["23","24","25","26","27","28","29","30"]

Query results immediately clear a lot of clarity!

I hope the method described in this paper can be helpful for everyone to learn ThinkPHP.


Related articles: