Сопоставление массивов в php
Вопрос
Мне нужно проверить, будет ли данный массив соответствовать другому массиву.Я не могу понять, как манипулировать первым массивом или сопоставить его каким-либо другим способом.
Мне нужно сопоставить этот массив:
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
со списком всех записей содержимого, найденных в $results, которые соответствуют индексам в $indexes.Надеюсь, это поможет.
$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;
}