foreach($A['cities'] as $v1){
foreach($A['cities'] as $v2){
if($v1==$v2) continue;
$s1 = $v1.''.$v2;
$s2 = $v2.''.$v1;
if(in_array($s1, $A['index'] ) || in_array($s2, $A['index'] )) continue;
$A['pairs'][] = array($v1,$v2);
$A['index'][] = $s1;
$A['index'][] = $s2;
}
}
SELECT`s1`.`title`,`s1`.`description`,`s1`.`contacts`, `count`, contacttitle
FROM `spec` AS `s1`
LEFT JOIN (
SELECT `contacts`, COUNT(`contacts`) AS `count`, CONCAT(`title`, '--', `description`) AS contacttitle
FROM `spec`
GROUP BY 1) AS `s2` USING (`contacts`)
WHERE `status`='' AND id>19000
ORDER BY id DESC;