我想在php for循环中创建一个单词组合 这是我的代码
$fromCity = 'LKO';
$toCity ='TRV';
for($c=0;$c<count($data);$c++)
{
$viacities = $data[$c]['citycode']; // Here i will get single connecting city Eg.BOM DEL MAA COK BLR
/* out put i will get as BOM DEL BLR MAA COK
* Now i want to create Route combinations as
- LKODEL, LKOBOM , LKOBLR , LKOMAA ,
LKOCOK , DELBOM , DELBLR , DELMAA,
DELCOK , BOMBLR , BOMMAA , BOMMAA ,
BLRMAA , BLRCOK , DELTRV , BOMTRV ,
BLRTRV , MAATRV , COKTRV
*/
$routCombinations = '**above combinations**'
$fetchRoutes = fetchRoutes($routCombinations);
}
如何在循环中创建这样的组合
答案 0 :(得分:0)
使用连接来连接字符串/值。 PHP中的operator为.
。以下是您在代码中使用它的方式。
$fromCity = 'LKO';
$toCity ='TRV';
$viacities = '';
for($c=0;$c<count($data);$c++) {
$viacities .= ' ' . $data[$c]['citycode']; // Here i will get single connecting city Eg.BOM DEL MAA COK BLR
$routCombinations = '**above combinations**'
$fetchRoutes = fetchRoutes($routCombinations);
}
这里有关于连接的更长时间的手册。
有两个字符串运算符。第一个是连接运算符(&#39;。&#39;),它返回其左右参数的串联。第二个是连接赋值运算符(&#39;。=&#39;),它将右侧的参数附加到左侧的参数。有关更多信息,请阅读分配运算符。
答案 1 :(得分:0)
我理解你的问题:
如何获得所有“A - &gt; B”组合,其中A和B是航线中的城市,A在B之前。
如果是这样,你可以这样做:
$fromCity = 'LKO';
$toCity ='TRV';
$allCities = array($fromCity);
foreach ($data as $viacity) {
$allCities[] = $viacity['citycode'];
}
$allCities[] = $toCity;
$routCombinations = array();
for ($i=0; $i<count($allCities); $i++) {
for ($j=i+1; $j<count($allCities); $j++) {
$routCombinations[] = $allCities[$i].$allCities[$j];
}
}
....
// If you need you can concatenate the array into a string :
$routCombinationsString = implode(',',$routCombinations)
....
$fetchRoutes = fetchRoutes($routCombinations);