题
我需要检查给定阵列是否将匹配另一个阵列。我不能找出如何可以操纵第一阵列,或以一些其它方式进行匹配。
我需要匹配此数组:
Array
(
[1] => site
[2] => blog
[3] => index.php
)
,以匹配这样的:
Array
(
[site] => Array
(
[path] => site
[name] => site
[kind] => directory
[content] => Array
(
[404] => Array
(
[path] => site/404.php
[name] => 404.php
[extension] => php
[kind] => file
)
[blog] => Array
(
[path] => site/blog
[name] => blog
[kind] => directory
[content] => Array
(
[contact] => Array
(
[path] => site/blog/contact.php
[name] => contact.php
[extension] => php
[kind] => file
)
[index] => Array
(
[path] => site/blog/index.php
[name] => index.php
[extension] => php
[kind] => file
)
[about] => Array
(
[path] => site/blog/about.php
[name] => about.php
[extension] => php
[kind] => file
)
)
)
[index] => Array
(
[path] => site/index.php
[name] => index.php
[extension] => php
[kind] => file
)
)
)
)
和返回该文件的内容数组:
[index] => Array
(
[path] => site/blog/index.php
[name] => index.php
[extension] => php
[kind] => file
)
帮助将不胜感激!
解决方案
只需索引到阵列:
$b[$a[1]]['content'][$a[2]]['content'][str_replace('.php', '', $a[3])]
如果您的输入可以是可变长度的,这样做在一个循环中,而不是
其他提示
所以,你需要...
$array2[$array[1]][content][$array[2]][substr(0, stripos($array[3], "."), $array[3])]
或者相近...
我可以认为这意味着你想要更大的阵列中的检查指标的列表并且返回可用/匹配任何结果?如果是这样,假设你的第一个数组被称为$indexes
和你的第二个被称为$results
你可以这样做:
$found = array();
foreach($indexes as $index) {
if(isset($results[$index])) {
$found[] = $results[$index]['content'];
}
}
和现在你必须$found
所有内容条目的列表中找到在$结果匹配在$索引的索引。希望帮助。
$path = array('site', 'blog', 'index.php');
$node = array(...); // the tree
for ($i = 0; $i < count($path) && !is_null($node); ++$i) {
$p = $path[$i];
$items = $i ? $node['content'] : $node;
$n = null;
foreach ($items as $it) {
if ($it['name'] == $p) {
$n = $node['content'][$p];
break;
}
}
$node = $n;
}
不隶属于 StackOverflow