减少阵列信息

时间:2016-04-15 10:13:38

标签: php arrays multidimensional-array

我从Web服务中提取了大量数据,这使我得到了一个多维混合数组,其中我只需要10%的信息。 我正在寻找一种灵活的方式来保留我的目的所需的信息。

我没有找到PHP函数或方法来做到这一点。

不要误解我的意思,我不想过滤价值观,我也不想对元素进行切片。

将其置于数据库视角,我想"简单地"删掉一些列。或者更好:我想保留我需要的列,并删除所有其他我不需要的列表,我也不知道。

E.G。

$big_array = array(
array("year" => 1979, "name" => "Miller",   "wage" => "100", "children"=>array("John", "Kate")),
array("year" => 1983, "name" => "Smith",  "wage" => "200"),
array("year" => 1980, "name" => "Mayer",  "wage" => "200", "children"=>array("Tom")),
array("year" => 1981, "name" => "Mayer",  "wage" => "100"),
array("year" => 1980, "name" => "Clinton", "wage" => "300", "children"=>array("Rosa", "Dick", "Christine")),
array("year" => 1981, "name" => "Bush", "wage" => "200"));
print_r($big_array);

会给我这个:

Array
(
    [0] => Array
        (
            [year] => 1979
            [name] => Miller
            [wage] => 100
            [children] => Array
                (
                    [0] => John
                    [1] => Kate
                )

        )

    [1] => Array
        (
            [year] => 1983
            [name] => Smith
            [wage] => 200
        )

    [2] => Array
        (
            [year] => 1980
            [name] => Mayer
            [wage] => 200
            [children] => Array
                (
                    [0] => Tom
                )

        )

    [3] => Array
        (
            [year] => 1981
            [name] => Mayer
            [wage] => 100
        )

    [4] => Array
        (
            [year] => 1980
            [name] => Clinton
            [wage] => 300
            [children] => Array
                (
                    [0] => Rosa
                    [1] => Dick
                    [2] => Christine
                )

        )

    [5] => Array
        (
            [year] => 1981
            [name] => Bush
            [wage] => 200
        )

)

现在出现了问题。我不知道我得到了哪些额外信息,因为我不是网络服务的主人。我只知道我需要的列。在这种情况下,我只需要" name"和#34;孩子"。而且我不想因为没有孩子而丢失数组元素。

因此返回的数组应如下所示:

Array
(
    [0] => Array
        (
            [name] => Miller
            [children] => Array
                (
                    [0] => John
                    [1] => Kate
                )

        )

    [1] => Array
        (
            [name] => Smith
        )

    [2] => Array
        (
            [name] => Mayer
            [children] => Array
                (
                    [0] => Tom
                )

        )

    [3] => Array
        (
            [name] => Mayer
        )

    [4] => Array
        (
            [name] => Clinton
            [children] => Array
                (
                    [0] => Rosa
                    [1] => Dick
                    [2] => Christine
                )

        )

    [5] => Array
        (
            [name] => Bush
        )

)

因此该函数看起来像这样:

$small_array=function($big_array, array("name","children"));

第二个参数包含要保留的键/列数组。

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

试试这个:

    $big_array = array(
array("year" => 1979, "name" => "Miller",   "wage" => "100", "children"=>array("John", "Kate")),
array("year" => 1983, "name" => "Smith",  "wage" => "200"),
array("year" => 1980, "name" => "Mayer",  "wage" => "200", "children"=>array("Tom")),
array("year" => 1981, "name" => "Mayer",  "wage" => "100"),
array("year" => 1980, "name" => "Clinton", "wage" => "300", "children"=>array("Rosa", "Dick", "Christine")),
array("year" => 1981, "name" => "Bush", "wage" => "200"));

$reqField = array('name','children');
$a = optimizeArray($big_array,$reqField);
echo "<pre>";
       print_r($a);

function optimizeArray($big_array,$reqField)
{
    $retArr = array();
    foreach($big_array as $mk=>$arr)
    {
        foreach($arr as $k=>$v)
        {
            if(in_array($k,$reqField) && isset($arr[$k]))
            {
               $retArr[$mk][$k] = $v;
            }

        }

    }
    return $retArr;
}

答案 1 :(得分:1)

您可以尝试此功能 -

$small_array = factor_array($big_array, array("name","children"));


/*
$main the big array
$keys the indexes to be extracted 
*/
function factor_array($main, $keys) {
    $temp = array();
    //loop through the big array
    foreach($main as $index => $array) {
        // loop through the keys array
        foreach($keys as $key) {
            if(isset($array[$key])) {// check if the key is set
                $temp[$index][$key] = $array[$key]; // store with the key
            }
        }

    }
    return $temp;
};

Fiddle

答案 2 :(得分:0)

好的,这两个答案对我的问题都是正确的。我要感谢你们俩。 但经过测试后,我意识到还有更深层的数组元素。 所以我想我们需要一个迭代的,递归的解决方案,这远远超出我的知识。

我得到了

$big_array = array(
array("year" => 1979, "name" => "Miller",   "wage" => "100", "children"=>array("John", "Kate")),
array("year" => 1983, "name" => "Smith",  "wage" => "200"),
array("year" => 1980, "name" => "Mayer",  "wage" => "200", "children"=>array("Tom")),
array("year" => 1981, "name" => "Mayer",  "wage" => "100"),
array("year" => 1980, "name" => "Clinton", "wage" => "300", "children"=>array("Rosa"=>array("Pet"=>array("Clover")), "Dick", "Christine")),
array("year" => 1981, "name" => "Bush", "wage" => "200"));
print_r($big_array);

给我

Array
(
    [0] => Array
        (
            [name] => Miller
            [children] => Array
                (
                    [0] => John
                    [1] => Kate
                )

        )

    [1] => Array
        (
            [name] => Smith
        )

    [2] => Array
        (
            [name] => Mayer
            [children] => Array
                (
                    [0] => Tom
                )

        )

    [3] => Array
        (
            [name] => Mayer
        )

    [4] => Array
        (
            [name] => Clinton
            [children] => Array
                (
                    [Rosa] => Array
                        (
                            [Pet] => Array
                                (
                                    [0] => Clover
                                )

                        )

                    [0] => Dick
                    [1] => Christine
                )

        )

    [5] => Array
        (
            [name] => Bush
        )

)

如何优化甚至多维数组呢?

答案 3 :(得分:0)

这是一个非常简单的操作:使用array_intersect_key将单个数组减少到仅指定的键,然后对数组中的所有数组执行此操作:

$reduced = array_map(function (array $data) {
    return array_intersect_key($data, array_flip(['name', 'children']));
}, $originalArray);