Created
November 7, 2023 13:55
-
-
Save joparara/89aaef4c2d564b33dde38dbbdcc212ce to your computer and use it in GitHub Desktop.
how to remove unnecessary fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$transactions = [ | |
["id" => 1, "name" => "John Smith", "x" => "y"], | |
["id" => 2, "name" => "Joe Adam", "z" => "a"], | |
]; | |
$fields = ["id", "name"]; | |
$callback = function ($transaction) use ($fields) { | |
$keys = array_keys($transaction); | |
foreach ($keys as $key) { | |
if (!in_array($key, $fields)) { | |
unset($transaction[$key]); | |
} | |
} | |
return $transaction; | |
}; | |
$transactions = array_map($callback, $transactions); | |
var_dump($transactions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment