从 php 多维数组中获取 friendly-url 的完整类别层次结构字符串
Getting the full category hierarchy string for a friendly-url from a php multidimensional array
我正在尝试从 未定义深度 的 php 多维数组中获取用于附加到友好 url 上的完整字符串。类似于 Wordpress 所做的。结果就像 parent/son/grandson
。这是我拥有的数组的示例:
Array
(
[0] => Array
(
[id] => 26
[slug] => animals
[children] => Array
(
[0] => Array
(
[id] => 29
[slug] => insects
[children] => Array
(
[0] => Array
(
[id] => 32
[slug] => grasshoppers
)
)
)
[1] => Array
(
[id] => 28
[slug] => mammals
[children] => Array
(
[0] => Array
(
[id] => 30
[slug] => dogs
)
[1] => Array
(
[id] => 31
[slug] => horses
)
)
)
)
)
[1] => Array
(
[id] => 27
[slug] => minerals
[children] => Array
(
[0] => Array
(
[id] => 33
[slug] => salt
)
)
)
)
我想调用这样的函数:
$full_slug = getFullSlug('dogs', $array);
$full_slug 的内容为:
"animals/mammals/dogs"
经过一些研究,我找到了在 递归方法 上使用 array_unshift 的类似案例的示例,但我无法提供我自己为这个特殊案例工作的东西。任何有关 getFullSlug() 函数的帮助都将不胜感激!
像这样的东西应该适合你:
public function getFullSlug($key, $array) {
foreach($array as $arrayKey => $arrayValue) {
if (is_array($arrayValue)) {
return $arrayKey . '/' . $this->getFullSlug($key, $arrayValue);
} else if ($arrayValue === $key) {
return $arrayValue;
}
throw InvalidArguementException("full slug could not be built for $key", 404);
}
}
使用递归很容易做到这一点:
function getFullSlug($key, $array) {
foreach($array as $node) {
if($key == $node['slug']) { //Found it on this node
return $node['slug'];
}
else //Search depth first
{
if (isset($node['children']) && is_array($node['children']) && !empty($node['children']) ) {
$subSlug = getFullSlug($key, $node['children']);
if(!empty($subSlug)) //If it was found
{
return $node['slug'].'/'.$subSlug;
}
else //Look at the next sibling
{
continue;
}
} else { // This is a leaf, and it wasn't found
continue;
}
}
}
return ''; //Failed to find it.
}
当然,这也可以添加到 class。如果这样做,则需要将 getFullSlug 调用更新为 $this->getFullSlug(....).
这是我测试过的 slugs 数组,应该与您提到的完全一样:
$slugs = array(
array(
'id' => 26,
'slug' => 'animals',
'children' => array(
array(
'id' => 29,
'slug' => 'insects',
'children' => array(
array(
'id' => 32,
'slug' => 'grasshoppers'
)
)
),
array(
'id' => 28,
'slug' => 'mammals',
'children' => array(
array(
'id' => 30,
'slug' => 'dogs'
),
array(
'id' => 31,
'slug' => 'horses'
)
)
)
)
),
array(
'id' => 27,
'slug' => 'minerals',
'children' => array(
array(
'id' => 33,
'slug' => 'salt'
)
)
)
);
最后,这里是一些使用函数的代码:
echo getFullSlug('animals', $slugs).'<br>';
echo getFullSlug('insects', $slugs).'<br>';
echo getFullSlug('grasshoppers', $slugs).'<br>';
echo getFullSlug('mammals', $slugs).'<br>';
echo getFullSlug('dogs', $slugs).'<br>';
echo getFullSlug('horses', $slugs).'<br>';
echo getFullSlug('minerals', $slugs).'<br>';
echo getFullSlug('salt', $slugs).'<br>';
和预期的相应输出:
animals
animals/insects
animals/insects/grasshoppers
animals/mammals
animals/mammals/dogs
animals/mammals/horses
minerals
minerals/salt
希望对您有所帮助!
P.S。要检查 slug 是否存在,您可以使用 empty().
对于上面的例子,
$url = getFullSlug('platypus', $slugs);
if(empty($url))
echo "Slug doesn't exist";
else
echo "New url: $url";
如果 $slugs 多维数组中不存在鸭嘴兽, 将打印 "Slug doesn't exist",如果确实存在,它将打印类似 "New url: animals/mammals/platypus" 的内容。
我正在尝试从 未定义深度 的 php 多维数组中获取用于附加到友好 url 上的完整字符串。类似于 Wordpress 所做的。结果就像 parent/son/grandson
。这是我拥有的数组的示例:
Array
(
[0] => Array
(
[id] => 26
[slug] => animals
[children] => Array
(
[0] => Array
(
[id] => 29
[slug] => insects
[children] => Array
(
[0] => Array
(
[id] => 32
[slug] => grasshoppers
)
)
)
[1] => Array
(
[id] => 28
[slug] => mammals
[children] => Array
(
[0] => Array
(
[id] => 30
[slug] => dogs
)
[1] => Array
(
[id] => 31
[slug] => horses
)
)
)
)
)
[1] => Array
(
[id] => 27
[slug] => minerals
[children] => Array
(
[0] => Array
(
[id] => 33
[slug] => salt
)
)
)
)
我想调用这样的函数:
$full_slug = getFullSlug('dogs', $array);
$full_slug 的内容为:
"animals/mammals/dogs"
经过一些研究,我找到了在 递归方法 上使用 array_unshift 的类似案例的示例,但我无法提供我自己为这个特殊案例工作的东西。任何有关 getFullSlug() 函数的帮助都将不胜感激!
像这样的东西应该适合你:
public function getFullSlug($key, $array) {
foreach($array as $arrayKey => $arrayValue) {
if (is_array($arrayValue)) {
return $arrayKey . '/' . $this->getFullSlug($key, $arrayValue);
} else if ($arrayValue === $key) {
return $arrayValue;
}
throw InvalidArguementException("full slug could not be built for $key", 404);
}
}
使用递归很容易做到这一点:
function getFullSlug($key, $array) {
foreach($array as $node) {
if($key == $node['slug']) { //Found it on this node
return $node['slug'];
}
else //Search depth first
{
if (isset($node['children']) && is_array($node['children']) && !empty($node['children']) ) {
$subSlug = getFullSlug($key, $node['children']);
if(!empty($subSlug)) //If it was found
{
return $node['slug'].'/'.$subSlug;
}
else //Look at the next sibling
{
continue;
}
} else { // This is a leaf, and it wasn't found
continue;
}
}
}
return ''; //Failed to find it.
}
当然,这也可以添加到 class。如果这样做,则需要将 getFullSlug 调用更新为 $this->getFullSlug(....).
这是我测试过的 slugs 数组,应该与您提到的完全一样:
$slugs = array(
array(
'id' => 26,
'slug' => 'animals',
'children' => array(
array(
'id' => 29,
'slug' => 'insects',
'children' => array(
array(
'id' => 32,
'slug' => 'grasshoppers'
)
)
),
array(
'id' => 28,
'slug' => 'mammals',
'children' => array(
array(
'id' => 30,
'slug' => 'dogs'
),
array(
'id' => 31,
'slug' => 'horses'
)
)
)
)
),
array(
'id' => 27,
'slug' => 'minerals',
'children' => array(
array(
'id' => 33,
'slug' => 'salt'
)
)
)
);
最后,这里是一些使用函数的代码:
echo getFullSlug('animals', $slugs).'<br>';
echo getFullSlug('insects', $slugs).'<br>';
echo getFullSlug('grasshoppers', $slugs).'<br>';
echo getFullSlug('mammals', $slugs).'<br>';
echo getFullSlug('dogs', $slugs).'<br>';
echo getFullSlug('horses', $slugs).'<br>';
echo getFullSlug('minerals', $slugs).'<br>';
echo getFullSlug('salt', $slugs).'<br>';
和预期的相应输出:
animals
animals/insects
animals/insects/grasshoppers
animals/mammals
animals/mammals/dogs
animals/mammals/horses
minerals
minerals/salt
希望对您有所帮助!
P.S。要检查 slug 是否存在,您可以使用 empty().
对于上面的例子,
$url = getFullSlug('platypus', $slugs);
if(empty($url))
echo "Slug doesn't exist";
else
echo "New url: $url";
如果 $slugs 多维数组中不存在鸭嘴兽,将打印 "Slug doesn't exist",如果确实存在,它将打印类似 "New url: animals/mammals/platypus" 的内容。