【问题标题】:Search multidimensional array in PHP and return keys在 PHP 中搜索多维数组并返回键
【发布时间】:2014-04-16 06:16:02
【问题描述】:

我面临以下问题:我已经建立了一个代表“图标库”的网站。它基本上显示了包含图标附加属性的 CSV 文件的内容,例如

  • 文件名
  • 工具提示
  • 标签
  • 状态(完成,进行中)
  • 文件类型
    • png
      • 在目录中找到文件名
    • 每股收益
      • 在目录中找到文件名
    • ico
      • 在目录中找到文件名
    • ...

最后一切都放入一个大数组中:

[390] => Array
    (
        [0] => hammer
        [1] => Properties
        [2] => tools, hammer, properties
        [3] => 
        [4] => done
        [png] => Array
            (
                [0] => hammer_16x.png
                [1] => hammer_32x.png
            )

        [eps] => Array
            (
                [0] => hammer_16x.eps
                [1] => hammer_32x.eps
            )

        [ico] => Array
            (
                [0] => hammer.ico
            )

    )

现在我想提供在这个数组中搜索并根据搜索结果过滤网站上显示的内容的可能性。因此,我想至少搜索以下字符串:

 [0] => hammer
 [1] => Properties
 [2] => tools, hammer, properties
 [3] => 
 [4] => done

任何提示我该怎么做? 非常感谢!

【问题讨论】:

  • 写一堆foreach循环,还有什么?

标签: php search multidimensional-array


【解决方案1】:

您可以将array_filterarray_diff_assoc 结合使用。 有点像这样:

function filter($array, $filter_elem) {
    return array_filter($array, function ($v) use($filter_elem) {
        return count(array_diff_assoc($filter_elem, $v)) == 0;
    });
}

$array = array(
    '390' => array(
        '0' => 'hammer',
        '1' => 'Properties',
        '2' => 'tools, hammer, properties',
        '3' => false,
        '4' => 'done',
        'png' => array(
            '0' => 'hammer_16x.png',
            '1' => 'hammer_32x.png',
        ),
        'eps' => array(
            '0' => 'hammer_16x.eps',
            '1' => 'hammer_32x.eps',
        ),
        'ico' => array(
            '0' => 'hammer.ico',
        ),
    ),
    ...
);

$filter_elem = array(
    '1' => 'Properties',
    '2' => 'tools, hammer, properties',
    '3' => false,
    '4' => 'done',
);

print_r(filter($array, $filter_elem));

Demo

【讨论】:

  • 嗨!这是一个很酷的方法:) 我测试过!我还想尝试使用通配符(例如 preg_filter?),以便在仅搜索例如时也显示上面的结果。 “嗯”。但是我现在使用您的代码尝试的方法不起作用:/
  • 这是另一个问题(问题)。我已经解决了您在原始问题中描述的问题。您必须首先承认我的回答是解决方案。然后你可以问另一个问题。
  • 你是对的。这个我没有明确提到!感谢您的支持!
  • 与这个问题相关我现在添加了这个额外的问题(特别关注正则表达式的使用):stackoverflow.com/questions/22381434/…
【解决方案2】:

一种优雅且可重复使用的方式。

实施:

class FilterableArrayCollection implements ArrayAccess
{
    /**
     *    The original $data from your array it contains the $items to be sorted.
     * The items in this array are sorted in the original order you received/read
     * them in.
     */
    protected $data  = array();

    /**
     *    A hash structured as key/subkeys/items
     * - the key is an index name -- the name of a property you have in each item
     *   from the original data;
     * - the subkeys are the value of the property from each item;
     * - the items are variable references to items who respect the rule
     *   $item[$key] === $subkey
     */
    protected $index = array();

    public function __construct(array $data = array())
    {
        $this->data = $data;
    }

    public function findBy($filter, $value, $isUnique = true)
    {
        if (!isset($this->index[$filter]) {
            foreach ($this->data as $item) {
                $currentValue = $item[$filter];

                if ($isUnique) {
                    $this->index[$filter][$currentValue] = &$item;
                } else {
                    $this->index[$filter][$currentValue][] = &$item;
                }
            }
            ksort($this->index[$filter]);
        } else {
            return($this->index[$filter][$value]);
        }
    }

/*
 * Implement ArrayAccess interface here
 */
}

示例:

$data = array(
    array(
        'filename' => 'hammer',
        'tooltip' => 'Properties',
        'tags' => 'tools, hammer, properties',
        'state' => 'done',
        'filetypes' => array(
            'png' => array(
                'hammer_16x.png',
                'hammer_32x.png'
            ),
            'eps' => array(
                'hammer_16x.eps',
                'hammer_32x.eps'
            ),
            'ico' => array(
                'hammer.ico'
            )
        )
    ),
    array(
        'filename' => 'hammer',
        'tooltip' => 'not-Properties',
        'tags' => 'tools, hammer, properties',
        'state' => 'in progress',
        'filetypes' => array(
            'png' => array(
                'hammer_16x.png',
                'hammer_32x.png'
            ),
            'eps' => array(
                'hammer_16x.eps',
                'hammer_32x.eps'
            ),
            'ico' => array(
                'hammer.ico'
            )
        )
    )
);

$x = new FilterableArrayCollection($data);
$filtered = $x->findBy('state', 'done');
var_dump($filtered);

/**
array(
        'filename' => 'hammer',
        'tooltip' => 'Properties',
        'tags' => 'tools, hammer, properties',
        'state' => 'done',
        'filetypes' => array(
            'png' => array(
                'hammer_16x.png',
                'hammer_32x.png'
            ),
            'eps' => array(
                'hammer_16x.eps',
                'hammer_32x.eps'
            ),
            'ico' => array(
                'hammer.ico'
            )
        )
    )
 */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2015-02-21
    相关资源
    最近更新 更多