2

I am pulling a large amount of data from a web service which gives me back a multidimensional mixed array where I only need 10% of information from. I am looking for a flexible way to keep only the information needed for my purposes.

I did not find a PHP function or method to do that.

Don't get me wrong, I don't want to filter values and I don't want to slice elements.

Putting it into a database perspective, I want to "simply" drop some columns. Or better: I want to keep columns I need and drop all the others which I don't need and I don't know.

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);

Will give me this:

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
        )

)

Now comes the problem. I have no clue what additional information I get as I am not the master of the web service. I only know about the columns I need. In this case I only need "name" and "children". And I don't want to lose array elements just because they have no children.

So a returned array should look like this:

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
        )

)

Hence the function should look something like this:

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

With the second argument containing an array of keys/columns to keep.

Any idea?

0

4 Answers 4

2

Try this:

    $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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

correct but user need function so you need to convert your code with function.
1

You can try this function -

$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

Comments

0

Ok, both answers are correct for my question. I want to thank you both. But after testing it I realized that there are even deeper array elements. So I guess we need an iterating, recursive solution which is far beyond my knowledge.

I got

$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);

Giving me

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
        )

)

How can I optimize even multidimensional arrays?

Comments

0

It's a pretty trivial operation: use array_intersect_key to reduce a single array to only the keys specified, then do this for all arrays in your array:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.