I'd like to know if there is any way to simplify this:
$transactions = $database->fetchAll("SELECT * FROM transactions WHERE user = :user", array(":user" => $USER["id"]));
$transactions_cleaned = array();
foreach($transactions as $transaction){
unset($transaction["id"]);
unset($transaction["user"]);
array_push($transactions_cleaned, $transaction);
}
It basically removes each id
and user
variables for each object of the following array:
Array
(
[0] => Array
(
[id] => 1
[user] => 1
[type] => deposit
[amount] => 1000
)
[1] => Array
(
[id] => 2
[user] => 1
[type] => withdraw
[amount] => 1000
)
)
So it becomes like this:
Array
(
[0] => Array
(
[type] => deposit
[amount] => 1000
)
[1] => Array
(
[type] => withdraw
[amount] => 1000
)
)
Thanks to mfort and also Sagar Sainkar even if yours is more complex:
$transactions = $database->fetchAll("SELECT * FROM transactions WHERE user = :user", array(":user" => $USER["id"]));
foreach($transactions as &$transaction){
unset($transaction["id"]);
unset($transaction["user"]);
}
Try this if you want do that with in one line and without using foreach
<?php
array_walk($transactions , function ($item, $key) use (&$transactions)
{
unset($transactions[$key]['id']);
unset($transactions[$key]['user']);
});
?>