简体   繁体   English

获取具有关键PHP值的数组集

[英]Get the array set which has a distinct value for key PHP

array(
        [0]=> array(3) 
            { 
                [0]=> array(3) 
                { 
                    ["name"]=> string(1) "a" ,
                    ["code"]=> string(3) "416" ,
                    ["id"]=> string(2) "a1" ,
                }, 
                [1]=> array(3)
                { 
                    ["name"]=> string(1) "a", 
                    ["code"]=> string(3) "522" ,
                    ["id"]=> string(2) "a2", 
                }, 
                [2]=> array(3) 
                { 
                    ["name"]=> string(1) "b" ,
                    ["code"]=> string(3) "580" ,
                    ["id"]=> string(2) "b1" ,
                }
            },
        [1]=> array(3) 
            { 
                [0]=> array(3) 
                { 
                    ["name"]=> string(1) "a" ,
                    ["code"]=> string(3) "416" ,
                    ["id"]=> string(2) "a1" ,
                }, 
                [1]=> array(3) 
                { 
                    ["name"]=> string(1) "a" ,
                    ["code"]=> string(3) "522" ,
                    ["id"]=> string(2) "a2" ,
                }, 
                [2]=> array(3) 
                { 
                    ["name"]=> string(1) "b" ,
                    ["code"]=> string(3) "899" ,
                    ["id"]=> string(2) "b2", 
                } 
            } 
    );

I have array like this. 我有这样的数组。 All I need is for each array (eg [0]=>array()) I will search for the arrays inside and get the array['code'] only for array set that has distinct name value. 我需要的是每个数组(例如[0] => array()),我将在其中搜索数组,并仅对具有不同名称值的数组集获取array['code']

Example for array[0]: I will get the array[0][2]['code'] because the array set of this particular array[0][2]['code'] has a unique 'name' array [0]的示例:我将得到array[0][2]['code']因为此特定array[0][2]['code']的数组集具有唯一的“名称”

Assume in the below code $array is $arr[0] from your example: 假设以下代码中的$array是示例中的$arr[0]

$array = array(
    array(
        "name" => "a",
        "code" => "416",
        "id" => "a1"
    ),
    array(
        "name" => "a",
        "code" => "522",
        "id" => "a2"
    ),
    array(
        "name" => "b",
        "code" => "580",
        "id" => "b1"
    )
);

$counts = array_count_values(
    array_map(function (array $entry) { return $entry['name']; }, $array)
    // or array_column($array, 'name') in PHP 5.5+
);

$uniqueNames = array_keys(
    array_filter($counts, function ($count) { return $count == 1; })
);

$result = array_filter($array, function (array $entry) use ($uniqueNames) {
    return in_array($entry['name'], $uniqueNames);
});

Not necessarily the super most efficient method, but straight forward and functional. 不一定是超级高效的方法,而是简单有效的方法。 It filters the array down to the entries where name exists only once. 它将数组向下过滤到name仅存在一次的条目。 This may or may not fit your definition of "unique", it's rather unclear what variations in the input data you may have. 这可能符合或可能不符合您对“唯一”的定义,目前尚不清楚您输入数据中可能有哪些变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM