在PHP中搜索多维数组并返回键

时间:2014-03-12 08:36:40

标签: php search multidimensional-array

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

  • 文件名
  • 工具提示
  • 标签
  • 状态(已完成,正在进行中)
  • 文件类型
    • PNG
      • 目录中的文件名
    • EPS
      • 目录中的文件名
    • 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

任何提示我怎么能这样做? 非常感谢!

2 个答案:

答案 0 :(得分:0)

您可以将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

答案 1 :(得分:0)

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

实现:

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'
            )
        )
    )
 */