如何从多维数组中获取某些值?

时间:2019-03-25 20:35:32

标签: php

这是我现在正在使用的数据:

    $city = [
        [
            "name" => "Dhaka",
            "areas" => ["d1", "d2", "d3"]
        ],
        [
            "name" => "Chittagong",
            "areas" => ["c1", "c2", "c3"]
        ],
        [
            "name" => "Sylhet",
            "areas" => ["s1", "s2", "s3"]
        ],
        [
            "name" => "Barisal",
            "areas" => ["b1", "b2", "b3"]
        ],
        [
            "name" => "Khulna",
            "areas" => ["k1", "k2", "k3"]
        ],
        [
            "name" => "Rajshahi",
            "areas" => ["r1", "r2", "r3"]
        ],
    ];

我想获取仅与“名称”键关联的数据列表。

一旦用户输入相应的“名称”,我还想显示与“区域”键关联的数据。

如果有关于如何更好地存储这些数据以便在代码中频繁使用的建议,将会很有帮助。

2 个答案:

答案 0 :(得分:3)

只要名称唯一,就可以执行以下简单操作:

示例1

$city = [
    [
        "name" => "Dhaka",
        "areas" => ["d1", "d2", "d3"]
    ],
    [
        "name" => "Chittagong",
        "areas" => ["c1", "c2", "c3"]
    ],
    [
        "name" => "Sylhet",
        "areas" => ["s1", "s2", "s3"]
    ],
    [
        "name" => "Barisal",
        "areas" => ["b1", "b2", "b3"]
    ],
    [
        "name" => "Khulna",
        "areas" => ["k1", "k2", "k3"]
    ],
    [
        "name" => "Rajshahi",
        "areas" => ["r1", "r2", "r3"]
    ],
];    

$names = array_column($city, null, 'name');

print_r($names);

输出

Array
(
    [Dhaka] => Array
        (
            [name] => Dhaka
            [areas] => Array
                (
                    [0] => d1
                    [1] => d2
                    [2] => d3
                )

        )

    [Chittagong] => Array
        (
            [name] => Chittagong
            [areas] => Array
                (
                    [0] => c1
                    [1] => c2
                    [2] => c3
                )

        )

    [Sylhet] => Array
        (
            [name] => Sylhet
            [areas] => Array
                (
                    [0] => s1
                    [1] => s2
                    [2] => s3
                )

        )

    [Barisal] => Array
        (
            [name] => Barisal
            [areas] => Array
                (
                    [0] => b1
                    [1] => b2
                    [2] => b3
                )

        )

    [Khulna] => Array
        (
            [name] => Khulna
            [areas] => Array
                (
                    [0] => k1
                    [1] => k2
                    [2] => k3
                )

        )

    [Rajshahi] => Array
        (
            [name] => Rajshahi
            [areas] => Array
                (
                    [0] => r1
                    [1] => r2
                    [2] => r3
                )

        )

)

现在您可以按名称查找

print_r($city['Chittagong']['areas']); //["c1", "c2", "c3"]

如果不能保证名称唯一,则必须使用foreach来构建它们,如下所示:

示例2

$city = [
        [
            "name" => "Dhaka",
            "areas" => ["d1", "d2", "d3"]
        ],
        [
            "name" => "Chittagong",
            "areas" => ["c1", "c2", "c3"]
        ],
         [ /*--- ADDED to show duplication ---- */
            "name" => "Chittagong",
            "areas" => ["x1", "x2", "x3"]
        ],
        [
            "name" => "Sylhet",
            "areas" => ["s1", "s2", "s3"]
        ],
        [
            "name" => "Barisal",
            "areas" => ["b1", "b2", "b3"]
        ],
        [
            "name" => "Khulna",
            "areas" => ["k1", "k2", "k3"]
        ],
        [
            "name" => "Rajshahi",
            "areas" => ["r1", "r2", "r3"]
        ],
    ];


$output = [];

foreach($city as $k=>$v){
    $k = $v['name'];
    if(!isset($output[$k])) $output[$k] = []; //initialize

    $output[$k][] = $v;
}

print_r($output);

哪个会给你这样的东西

Array
(
    [Dhaka] => Array
        (
            [0] => Array
                (
                    [name] => Dhaka
                    [areas] => Array
                        (
                            [0] => d1
                            [1] => d2
                            [2] => d3
                        )

                )

        )

    [Chittagong] => Array
        (
            [0] => Array
                (
                    [name] => Chittagong
                    [areas] => Array
                        (
                            [0] => c1
                            [1] => c2
                            [2] => c3
                        )

                )

            [1] => Array
                (
                    [name] => Chittagong
                    [areas] => Array
                        (
                            [0] => x1
                            [1] => x2
                            [2] => x3
                        )

                )

        )

    [Sylhet] => Array( ... )

如您所见,这会添加一个额外的级别来包含多个事件。当然,当重复时,您可以只合并area(在foreach中)。但这取决于您。

以下是一个简单的示例(与上述数据相同):

示例3

$output = [];

foreach($city as $k=>$v){
    $k = $v['name'];
    if(!isset($output[$k])) $output[$k] = []; //initialize

    $output[$k] = array_merge($output[$k], $v['areas']); //merge the areas
}

print_r($output);

输出

Array
(
    [Dhaka] => Array
        (
            [0] => d1
            [1] => d2
            [2] => d3
        )

    [Chittagong] => Array
        (
            [0] => c1
            [1] => c2
            [2] => c3
            [3] => x1
            [4] => x2
            [5] => x3
        )
  ....
  )

如果是我,我会选择最后一个,只是因为它将在以后使用它的简单性。

PS 。如果您使用PDO从数据库中提取数据,则可以使用$stmt->fetchAll(PDO::FETCH_GROUP),它会自动为您提供类似于第二个示例的内容。唯一需要注意的是,name列是查询中要选择的第一个列,它将是该列的分组。

干杯。

答案 1 :(得分:2)

如果您只是试图遍历城市列表并获取名称,则可以使用foreach循环。如果您还需要其他功能,请尝试定义更多问题。

foreach ($city as $c) {
        echo $c["name"];
    }