我使用我的简单项目从另一个php文件获取变量名。一些变量具有相同的值..
我使用foreach在特定目录中的不同php文件中显示变量..
(1st app_config.php) $app_name = "Pass Generator";
(2nd app_config.php) $app_name = "Random Name Generator";
(3rd app_config.php) $app_name = "Love Meter";
(4th app_config.php) $app_name = "Random Name Generator";
(5th app_config.php) $app_name = "Lucky Number Generator";
由于$ app_name的第2和第4个变量具有相同的值,因此如何跳过其中一个。所以输出将是:
Pass Generator
Random Name Generator
Love Meter
Lucky Number Generator
这是我的代码:
$path = '../../apps/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
require_once("../../apps/".$result."/app_config.php");
$app .= $app_name."<Br>";
}
}
echo $app_name;
任何?感谢
答案 0 :(得分:1)
$path = '../../apps/' . $name[0];
$results = scandir($path);
$arrProcessed = array();
foreach ($results as $result) {
if ($result === '.' or $result === '..' or array_key_exists($result, $arrProcessed)) continue;
$arrProcessed[$result] = true;
if (is_dir($path . '/' . $result)) {
require_once("../../apps/".$result."/app_config.php");
$app .= $app_name."<Br>";
}
}
echo $app_name;
答案 1 :(得分:1)
作为替代方案,您可以将它们收集在一个数组中,然后使用linebreak进行内爆/粘合:
$path = '../../apps/' . $name[0];
$results = scandir($path);
$apps = array();
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
require_once("../../apps/".$result."/app_config.php");
$apps[$app_name] = null;
}
}
echo implode('<br/>', array_keys($apps));
或另一种变体:
if (is_dir($path . '/' . $result)) {
require_once("../../apps/".$result."/app_config.php");
$apps[] = $app_name;
}
}
echo implode('<br/>', array_unique($apps));