You can select multiple counts from the table based on a condition using a CASE WHEN statement, and when writing SQL or something where formatting helps readability it's normally a good idea to use heredoc.
$getSQL = <<<SQL
COUNT(DISTINCT guid) AS allNum,
COUNT(DISTINCT CASE WHEN chk='Y' THEN guid END) AS openNum
I think there's three big things you might be able to do to improve the performance of this.
Write your query such that it doesn't need to execute the subquery for each line and/or have a HAVING clause by using an inner join which will automatically exclude those with no match.
Use ST_Distance_Sphere(POINT(:longitude, :latitude), POINT(cities.longitude, ...
This monster of a function is hard to use and hard to maintain. Besides, it is bloated with duplicated and/or unnecessary code. Last but not least, the way require "dbconn.script.php"; is called will make your code run slower and make some features (such as transactions) unavailable.
Too many connections
Your current code is connecting to the database ...
Array combine takes Keys + Values as values and combines them. Make sure they are the same size.
$result = array_combine(
And also a nice trick with array_map() and NOT using a callback.
$test1 = [
$test2 = [