Matching arrays in php
I need check whether a given array will match another array. I can't figure out how to either manipulate the first array, or to match it some other way.
I need to match this array:
Array
(
[1] => site
[2] => blog
[3] => index.php
)
to match this:
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
)
)
)
)
And return the file's content array:
[index] => Array
(
[path] => site/blog/index.php
[name] => index.php
[extension] => ph开发者_高级运维p
[kind] => file
)
Help would be greatly appreciated!
Just index into the array:
$b[$a[1]]['content'][$a[2]]['content'][str_replace('.php', '', $a[3])]
If your input can be variable length, do this in a loop instead.
So you need...
$array2[$array[1]][content][$array[2]][substr(0, stripos($array[3], "."), $array[3])]
Or something close...
Can I take that to mean you want to check for that list of indexes in the larger array and return any results that are available/matching? If so, and assuming your first array is called $indexes
and your second is called $results
you could do:
$found = array();
foreach($indexes as $index) {
if(isset($results[$index])) {
$found[] = $results[$index]['content'];
}
}
and now you'd have $found
with a list of all of the content entries found in $results that matched indexes in $indexes. Hope that helps.
$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;
}
精彩评论