douqi2571 2019-08-06 06:14 采纳率: 0%
浏览 80
已采纳

取消设置每个数组对象的变量

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
        )
)
  • 写回答

2条回答 默认 最新

  • dre75230 2019-08-06 06:53
    关注

    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"]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM