如何递归地从数组中减去?

时间:2021-12-11 13:42:00

I'm pulling the url parameter and I'm trying to create a breadcrumb out of it. So, if I pull:

我拉出url参数,并试图从中创建一个面包屑。所以,如果我把:

$url = 'contact/jane/now';

and I do:

我做的事:

$path = explode("/",$url);

How can I put it in a loop so that it breaks each part into a path like this:

我如何将它放入一个循环中,使它将每个部分分解成这样的路径:

<a href="/contact">contact</a>
<a href="/contact/jane">jane</a>
<a href="/contact/jane/now">now</a>

4 个解决方案

#1


4  

$answer = '';
foreach ($path as $path_part){
 $answer .= '/'.$path_part;
 print('<a href="'.$answer.'">'.$path_part.'</a>');
}

#2


2  

Pre PHP 5.3:

PHP 5.3之前:

array_reduce($path, create_function('$a, $b', '
    echo "<a href=\"$a/$b\">$b</a>\n";

    return "$a/$b";
'));

In PHP 5.3:

在PHP 5.3:

array_reduce($path, function($a, $b) {
    echo "<a href=\"$a/$b\">$b</a>\n";

    return "$a/$b";
});

#3


0  

You can use a For, For Each, Do, Do While, While. Really whatever you want. http://php.net/manual/en/control-structures.for.php When your explode it turns it into an array of strings http://us.php.net/manual/en/function.explode.php

你可以用For, For Each, Do, Do While, While。任何你想要的。当你的炸弹爆炸时,它会变成一个字符串数组:http://us.php.net/manual/en/function.。php。

So, just use the count() method and determine how many elements are in the array http://us.php.net/manual/en/function.count.php

因此,只需使用count()方法并确定数组http://us.php.net/manual/en/function.count.php中有多少元素

And then go ahead and loop through and read one at a time. If you want to start at the last and then go backwards you simply change your loop to go initialize at the count of the array then subtract one each time from your counter. Otherwise, just do it as normal.

然后循环,一次读一个。如果你想从最后一个开始,然后往回走,你只需改变你的循环去初始化数组的计数,然后每次从计数器中减去一个。否则,就像平常一样做。

Yay for looking on the PHP doc!

非常感谢您查找PHP文档!

#4


0  

You could use array_slice for that the code is not really tested but I hope you get the idea

您可以使用array_slice,因为代码没有经过真正的测试,但我希望您能理解。

...
$url   = explode('/', $url);
$links = array();

for ($i = 0; $i < count($url); $i++) {
    $crumb    = ($i > 0) ? implode('/', array_slice($url, 0, $i)) .'/' : $url[$i] .'/';
    $links[]  = sprintf('<a href="%s">%s</a>', $crumb . $url[$i], $url[$i]);
}

#1


4  

$answer = '';
foreach ($path as $path_part){
 $answer .= '/'.$path_part;
 print('<a href="'.$answer.'">'.$path_part.'</a>');
}

#2


2  

Pre PHP 5.3:

PHP 5.3之前:

array_reduce($path, create_function('$a, $b', '
    echo "<a href=\"$a/$b\">$b</a>\n";

    return "$a/$b";
'));

In PHP 5.3:

在PHP 5.3:

array_reduce($path, function($a, $b) {
    echo "<a href=\"$a/$b\">$b</a>\n";

    return "$a/$b";
});

#3


0  

You can use a For, For Each, Do, Do While, While. Really whatever you want. http://php.net/manual/en/control-structures.for.php When your explode it turns it into an array of strings http://us.php.net/manual/en/function.explode.php

你可以用For, For Each, Do, Do While, While。任何你想要的。当你的炸弹爆炸时,它会变成一个字符串数组:http://us.php.net/manual/en/function.。php。

So, just use the count() method and determine how many elements are in the array http://us.php.net/manual/en/function.count.php

因此,只需使用count()方法并确定数组http://us.php.net/manual/en/function.count.php中有多少元素

And then go ahead and loop through and read one at a time. If you want to start at the last and then go backwards you simply change your loop to go initialize at the count of the array then subtract one each time from your counter. Otherwise, just do it as normal.

然后循环,一次读一个。如果你想从最后一个开始,然后往回走,你只需改变你的循环去初始化数组的计数,然后每次从计数器中减去一个。否则,就像平常一样做。

Yay for looking on the PHP doc!

非常感谢您查找PHP文档!

#4


0  

You could use array_slice for that the code is not really tested but I hope you get the idea

您可以使用array_slice,因为代码没有经过真正的测试,但我希望您能理解。

...
$url   = explode('/', $url);
$links = array();

for ($i = 0; $i < count($url); $i++) {
    $crumb    = ($i > 0) ? implode('/', array_slice($url, 0, $i)) .'/' : $url[$i] .'/';
    $links[]  = sprintf('<a href="%s">%s</a>', $crumb . $url[$i], $url[$i]);
}