如何根据允许的键过滤多维数组

时间:2014-02-10 11:57:02

标签: php arrays

好的,这是我面临的挑战,这是一个非常复杂的挑战:

  • 我有一个多维数组(它可以只是一个数组,也可以深入几个级别)
  • 我有一组“允许”键(包含“parent.child”键)
  • 在最后一个数组中,我们只需要数字键和允许的键。所有其余的都必须从决赛桌上删除。

示例初始数组:

Array
(
    [0] => Array
        (
            [Title] => La science des rêves
            [Year] => 2006
            [Director] => Array
                (
                    [Name] => Michel Gondry
                    [BirthYear] => 1963
                    [BirthPlace] => Versailles, Yvelines, France
                )

        )

    [1] => Array
        (
            [Title] => Arizona Dream
            [Year] => 1992
            [Director] => Array
                (
                    [Name] => Emir Kusturica
                    [BirthYear] => 1954
                    [BirthPlace] => Sarajevo, Bosnia and Herzegovina, Yugoslavia
                )

        )

)

示例允许的密钥:

Array
(
    [0] => Title
    [1] => Director.Name
    [2] => Director.BirthYear
)

我想过滤它,以便最终的数组只包含上面的键(数字键将保持无关紧要):

Array
(
    [0] => Array
        (
            [Title] => La science des rêves
            [Director.Name] => Michel Gondry
            [Director.BirthYear] => 1963
        )

    [1] => Array
        (
            [Title] => Arizona Dream
            [Director.Name] => Emir Kusturica
            [Director.BirthYear] => 1954
        )
)

关于如何做到这一点的任何想法?我一直在努力工作几个小时,有各种各样的递归功能,但我总是看似错过了什么......:S

3 个答案:

答案 0 :(得分:1)

首先,显而易见的是,你需要一个可以压缩任意嵌套数组的函数。当你这样做时,如果输入被设计为产生它们,那么总是存在键碰撞的可能性,但是在你的情况下这不是问题所以你可以使用像

这样的东西。
function flatten($array, $separator = '.') {
    foreach ($array as $key => $value) {
        if (!is_array($value)) {
            continue;
        }

        unset ($array[$key]);

        foreach (flatten($value, $separator) as $subkey => $subval) {
            $array[$key.$separator.$subkey] = $subval;
        }

    }

    return $array;
}

现在,您可以使用以下内容展平每个“初始”行:

$flattened = array_map('flatten', $initial);

这会留下根据您的白名单过滤掉密钥的问题,您可以使用

$whitelist = array_flip(['Title', 'Director.Name', /* etc */]);

$filter = function($array) use (&$whitelist) {
    return array_intersect_key($array, $whitelist);
};

$filtered = array_map($filter, $flattened);

答案 1 :(得分:1)

<?php 

$array  =  array(Array(

                    "Title" => 'La science des rêves',
                    "Year" => '2006',
                    'Director' => Array
                    (
                        'Name' => 'Michel Gondry',
                        'BirthYear' => '1963',
                        'BirthPlace' => 'Versailles, Yvelines, France',
                    )

                ),

                Array
                    (
                        'Title' => 'Arizona Dream',
                        'Year' => '1992',
                        'Director' => Array
                            (
                                'Name' => 'Emir Kusturica',
                                'BirthYear' => '1954',
                                'BirthPlace' => 'Sarajevo, Bosnia and Herzegovina, Yugoslavia'
                            )
                    )

            );


/*
            [Title] => La science des rêves
            [Director.Name] => Michel Gondry
            [Director.BirthYear] => 1963*/


echo '<pre>';
print_r($array);
echo '</pre>';

foreach($array as $final)
{

    $final_array[]    =   array(
                                'title'=>$final['Title'], 
                                'name'=>$final['Director']['Name'],
                                'birthyear'=> $final['Director']['BirthYear']
                            );

}

echo '<pre>';
print_r($final_array);
echo '</pre>';

答案 2 :(得分:1)

试试这个

$arr=Array
(
    0 => Array
        (
            "Title" => "La science des rêves",
            "Year" => "2006",
            "Director" => Array
                (
                    "Name" => "Michel Gondry",
                    "BirthYear" => "1963",
                    "BirthPlace" => "Versailles, Yvelines, France"
                )

        ),

    1 => Array
        (
            "Title" => "Arizona Dream",
            "Year" => "1992",
            "Director" => Array
                (
                    "Name" => "Emir Kusturica",
                    "BirthYear" => "1954",
                    "BirthPlace" => "Sarajevo, Bosnia and Herzegovina, Yugoslavia"
                )

        )

);

$ck=Array
(
    0 => "Title",
    1 => "Director.Name",
    2 => "Director.BirthYear"
);


$newarray=array(array());
$s=sizeof($arr);
$ss=0;

    while($ss<$s)
    {
        foreach($ck as $ck2)
{
$k = explode(".",$ck2);
if(sizeof($k) > 1)
    $newarray[$ss][$ck2]=$arr[$ss][$k[0]][$k[1]];
        else
    $newarray[$ss][$ck2]=$arr[$ss][$k[0]];


    }$ss++;
}

print_r($newarray);

Demo