【发布时间】:2020-01-22 15:23:28
【问题描述】:
我已经复制了我在 php 中使用的函数的 sn-p 以获取数组并使用 implode 函数将其转换为 sql where 子句。我希望能够在 Android Java 代码中重复此功能。
换句话说,将一个 json 数组传递给一个函数,并让它执行与下面的 implode 函数等效的操作,以在查询字符串中创建 where 子句。 Android 中的 Java 是否具有“内爆”等价物,或者任何人都能够提供一个优雅的解决方案来帮助从 Android Java 中的 json 数组创建 where 子句? json 数组的格式为 {"var1":"val1", "var2":"val2"}
// select from fact sighings - requires an array to do the where clause
public function select_from_fact_sighting($whereArray) {
$jsonArray = json_decode($whereArray, true);
$elementCount = count($jsonArray);
$where = array();
foreach($jsonArray as $row) {
foreach($row as $key => $val) {
$qval = $this -> quote($val);
$where[] = $key . " = " . $qval;
}
}
if (!empty($where))
$query = sprintf('SELECT * FROM fact_sightings WHERE %s', implode(' AND ', $where)');
【问题讨论】: