在多维数组中搜索(PHP)

时间:2010-11-04 13:18:24

标签: php

我最近遇到的问题让我发疯了。我有一个像这样的多维数组:

$a = array(
'db' => array(
    '0' => array(
        'id' => '1',
        'name' => 'test',
        'cat' => array(
            'a' => '15',
            'b' => '20',
            'c' => '30'
        ),
        'canvas' => '2'
    ),
    '1' => array(
        'id' => '2',
        'name' => 'test2',
        'cat' => array(
            'a' => '15',
            'b' => '20',
            'c' => '30'
        ),
        'canvas' => '2'
    )
    '2' => array(
        'id' => '3',
        'name' => 'test',
        'cat' => array(
            'a' => '50',
            'b' => '40',
            'c' => '90'
        ),
        'canvas' => '1'
    )
)
);

我想用这样的函数搜索它:search('canvas = 1');

这将返回所有数组,db的子节点,它具有值为1的键画布。或者,例如:

搜索('a = 15');

将返回所有具有密钥的数组,cat的子代,名为a且值为15.

3 个答案:

答案 0 :(得分:3)

$a = array(
'db' => array(
    '0' => array(
        'id' => '1',
        'name' => 'test',
        'cat' => array(
            'a' => '15',
            'b' => '20',
            'c' => '30'
        ),
        'canvas' => '2'
    ),
    '1' => array(
        'id' => '2',
        'name' => 'test2',
        'cat' => array(
            'a' => '15',
            'b' => '20',
            'c' => '30'
        ),
        'canvas' => '2'
    ),
)
);

//checks if array $array contains element with $searchKey key, and $searchVal value
function arrayContains($array, $searchVal, $searchKey) {
        if (!is_array($array))
            return false;
        foreach ($array as $key => $value) {
            if ($key === $searchKey && $searchVal === $value)
                return true;
            if (is_array($value) && arrayContains($value, $searchVal, $searchKey))
                return true;
        }
        return false;
    }

function search($a, $search) {
    list($searchKey, $searchVal) = explode('=', $search);
    $result = array();
    foreach($a as $val) {
        if (arrayContains($val, $searchVal, $searchKey))
            $result[] = $val;
    }
    return $result;
}

print_r(search($a['db'], "a=15"));
print_r(search($a['db'], "canvas=1"));

产生此输出(输出$ a ['db']的子数组,其中包含搜索的key =>值对):

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => test
            [cat] => Array
                (
                    [a] => 15
                    [b] => 20
                    [c] => 30
                )

            [canvas] => 2
        )

    [1] => Array
        (
            [id] => 2
            [name] => test2
            [cat] => Array
                (
                    [a] => 15
                    [b] => 20
                    [c] => 30
                )

            [canvas] => 2
        )

)
Array
(
    [0] => Array
        (
            [id] => 3
            [name] => test
            [cat] => Array
                (
                    [a] => 50
                    [b] => 40
                    [c] => 90
                )

            [canvas] => 1
        )

)

答案 1 :(得分:1)

如果这可以帮助您,请查看以下链接 - http://php.net/manual/en/function.array-search.php

它包含php函数array_search()的详细文档以及用于在多维数组中搜索以及用户评论的各种用户代码。

答案 2 :(得分:0)

function search($array, $canvas)
{
    $result = array();
    foreach ($array as $k1 => $v1) {
        foreach ($v1 as $k2 => $v2) {
            if ($v2['canvas'] == $canvas) {
                $result[] = $array[$k1][$k2];
            }
        }
    }
    return $result;
}

// $a = your array
print_r(search($a, 1));