0

I working with my simple project which gets the variable name from another php file. Some variables has same value..

I used foreach to display variables in different php files in specific directory..

(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";

Since my 2nd and 4th variable of $app_name has same value, how can I skip one of them. So the output will be:

Pass Generator
Random Name Generator
Love Meter
Lucky Number Generator

This is my code:

$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;

Anyone? Thanks

user3232086
  • 11
  • 1
  • 5
  • 2
    I would suggest you http://php.net/manual/en/function.array-unique.php before the foreach loop(ps: foreach is slower than while). – HellBaby Dec 07 '14 at 04:06

2 Answers2

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;
Sebas
  • 21,192
  • 9
  • 55
  • 109
1

As an alternative, you could gather them inside an array then implode/glue them with 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));

Or another variation:

    if (is_dir($path . '/' . $result)) {
        require_once("../../apps/".$result."/app_config.php");
        $apps[] = $app_name;
    }
}

echo implode('<br/>', array_unique($apps));
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • `
    `, not `
    ` :-)
    – Sebas Dec 07 '14 at 04:04
  • 1
    @Sebas will both work, with or without the self closing. im just used to it :) – Kevin Dec 07 '14 at 04:07
  • According to the xml standard it will be
    and not
    cause it needs a closing tag. But a smartass who was smoked decided to dump the closing tag in html 5( your
    will not validate in html 4 and from what I saw not so many browsers offer support for stuffs introduced in 5)
    – HellBaby Dec 07 '14 at 04:10
  • it's html, not xml. html5 specs explicitely say it may not use a closing tag. http://www.w3.org/TR/html5/text-level-semantics.html#the-br-element – Sebas Dec 07 '14 at 05:30