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?