【问题标题】:Matching arrays in phpphp中的匹配数组
【发布时间】:2010-02-13 07:30:04
【问题描述】:

我需要检查一个给定的数组是否会匹配另一个数组。我不知道如何操作第一个数组,或者以其他方式匹配它。

我需要匹配这个数组:

    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
                                )

我们将不胜感激!

【问题讨论】:

    标签: php arrays match recursion


    【解决方案1】:

    只需索引到数组中:

    $b[$a[1]]['content'][$a[2]]['content'][str_replace('.php', '', $a[3])]
    

    如果您的输入可以是可变长度,请改为在循环中执行此操作。

    【讨论】:

      【解决方案2】:

      所以你需要...

      $array2[$array[1]][content][$array[2]][substr(0, stripos($array[3], "."), $array[3])]
      

      或者类似的东西......

      【讨论】:

        【解决方案3】:

        我可以认为这意味着您要检查较大数组中的索引列表并返回任何可用/匹配的结果吗?如果是这样,并假设您的第一个数组称为$indexes,而您的第二个数组称为$results,您可以这样做:

        $found = array();
        foreach($indexes as $index) {
          if(isset($results[$index])) {
            $found[] = $results[$index]['content'];
          }
        }
        

        现在您将拥有$found,其中包含在 $results 中找到的与 $indexes 中的索引匹配的所有内容条目的列表。希望对您有所帮助。

        【讨论】:

          【解决方案4】:
          $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;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-27
            • 1970-01-01
            相关资源
            最近更新 更多