PHP:用算法简化?

发布于 2024-09-05 12:46:46 字数 687 浏览 2 评论 0原文

这是一段我认为不是很“漂亮”的 PHP 代码,我确信可以用 for 或其他东西来简化它。我正在尝试找到适用于此的算法,但我无法弄清楚,请帮助我。

这是代码:

if(isset($four))
{
 if(isset($navi[$one][$two][$three][$four])) echo "/content/" . $one . "/" . $two . "/" . $three . "/" .$four . ".php";
 else echo "error";
}
else if(isset($three))
{
 if(isset($navi[$one][$two][$three]))  echo "/content/" . $one . "/" . $two . "/" . $three . ".php";
 else echo "error";
}
else if(isset($two))
{
 if(isset($navi[$one][$two])) echo "/content/" . $one . "/" . $two . ".php";
 else echo "error";
}
else if(isset($one))
{
 if(isset($navi[$one]))echo "/content/" . $one . ".php";
 else echo "error";
}
else
{
 echo "error";
}

谢谢!

Here's a piece of PHP code I think is not very "pretty", I'm sure it's possible to simplify it with for or something. I'm trying to find and algorithm that would work for this, but I can't figure it out, please help me.

Here's the code:

if(isset($four))
{
 if(isset($navi[$one][$two][$three][$four])) echo "/content/" . $one . "/" . $two . "/" . $three . "/" .$four . ".php";
 else echo "error";
}
else if(isset($three))
{
 if(isset($navi[$one][$two][$three]))  echo "/content/" . $one . "/" . $two . "/" . $three . ".php";
 else echo "error";
}
else if(isset($two))
{
 if(isset($navi[$one][$two])) echo "/content/" . $one . "/" . $two . ".php";
 else echo "error";
}
else if(isset($one))
{
 if(isset($navi[$one]))echo "/content/" . $one . ".php";
 else echo "error";
}
else
{
 echo "error";
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久随 2024-09-12 12:46:46

更新、测试:

$parts = array($one, $two, $three, $four);
$reversed = array_reverse($parts);

function getPath($ret, $n) {
    global $parts;
    foreach (range(0, $n) as $i) {
        $ret = $ret[$parts[$i]];
    }
    return $ret;
}

$error = false;
foreach (range(0, count($reversed)) as $i) {
    if (!$reversed[$i]) {
        unset($reversed[$i]);
        continue;
    }

    if (!getPath($navi, count($parts) - $i - 1)) {
        $error = true;
        break;
    }
}

if ($error) {
    echo "error!";
} else {
    echo "/content/" . implode("/", array_reverse($reversed)) . ".php";
}

Updated, tested:

$parts = array($one, $two, $three, $four);
$reversed = array_reverse($parts);

function getPath($ret, $n) {
    global $parts;
    foreach (range(0, $n) as $i) {
        $ret = $ret[$parts[$i]];
    }
    return $ret;
}

$error = false;
foreach (range(0, count($reversed)) as $i) {
    if (!$reversed[$i]) {
        unset($reversed[$i]);
        continue;
    }

    if (!getPath($navi, count($parts) - $i - 1)) {
        $error = true;
        break;
    }
}

if ($error) {
    echo "error!";
} else {
    echo "/content/" . implode("/", array_reverse($reversed)) . ".php";
}
百思不得你姐 2024-09-12 12:46:46

问题是您正在使用 isset(foo),这使得很难将变量放入数组中。如果测试变量的长度就可以了,那么使用:

$parts = array_reverse(array($one,$two,$three,$four));
foreach ($parts as $i => $value) 
    if(strlen($value)==0) 
        unset($array[$i]);
$final = join('/',parts);
if(isset($navi[$final])) echo "/content/" . $final . ".php";
else echo "\nerror\n\n\n";

但这需要您将 $navi 构造更改为看起来像

$navi['foo/bar/baz'] = "someval";

而不是

$navi['foo']['bar']['baz'] = "someval";

因为我们将使用连接字符串 $final 来查找$isset。否则唯一的路线是动态生成的 php,这是可怕的。

您能否以不同的方式构建数据?循环获取数组维度并不好,而上面的平面列表更容易完成。

The problem is that you are using isset(foo), which makes it hard to put the variables into an array. If testing the length of the variable will do, then use:

$parts = array_reverse(array($one,$two,$three,$four));
foreach ($parts as $i => $value) 
    if(strlen($value)==0) 
        unset($array[$i]);
$final = join('/',parts);
if(isset($navi[$final])) echo "/content/" . $final . ".php";
else echo "\nerror\n\n\n";

But this will require you to change your $navi construct to look like

$navi['foo/bar/baz'] = "someval";

instead of

$navi['foo']['bar']['baz'] = "someval";

because we will be using the concatenated string $final to look up in the $isset. Otherwise the only route is dynamically generated php which is horrible.

Could you structure your data differently? Looping for array dimensions is not nice, whereas the above flat list is much more easily accomplished.

素年丶 2024-09-12 12:46:46

我认为您的代码存在更深层次的问题。但为了解决这个问题——我的猜测是:

$urls = array();
$tempNavi = $navi;
foreach (array('one', 'two', 'three', 'four') as $var) {
    if (!isset($var) || !isset($tempNavi[$var]))
        break;
    $tempNavi = $tempNavi[$var];
    $urls[] = $var;
}

if ($urls) {
    echo '/content/' . implode('/', $urls);
} else {
    echo 'error';
}

I think there is deeper problems with Your code. But to solve just this problem -- there is my guess:

$urls = array();
$tempNavi = $navi;
foreach (array('one', 'two', 'three', 'four') as $var) {
    if (!isset($var) || !isset($tempNavi[$var]))
        break;
    $tempNavi = $tempNavi[$var];
    $urls[] = $var;
}

if ($urls) {
    echo '/content/' . implode('/', $urls);
} else {
    echo 'error';
}
岁月打碎记忆 2024-09-12 12:46:46

为了完整起见,递归解决方案:

function navi_recurse(&$navi, &$steps, $i = 0) {
  if ($i < count($steps) - 1) {
    $step = $steps[$i];
    if ( isset($navi[$step]) )
      return navi_recurse($navi[$step], $steps, $i+1);
    else
      return "error\n";
  } 
  return '/content/'.implode('/', $steps).'.php';
}

像这样调用:

$steps = array($one, $two, $three, $four);
echo navi_recurse($navi, $steps);

A recursive solution, for the sake of completeness:

function navi_recurse(&$navi, &$steps, $i = 0) {
  if ($i < count($steps) - 1) {
    $step = $steps[$i];
    if ( isset($navi[$step]) )
      return navi_recurse($navi[$step], $steps, $i+1);
    else
      return "error\n";
  } 
  return '/content/'.implode('/', $steps).'.php';
}

Call like this:

$steps = array($one, $two, $three, $four);
echo navi_recurse($navi, $steps);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文