【问题标题】:Compare a value in array in php - if not exist then add that to json array比较 php 中数组中的值 - 如果不存在,则将其添加到 json 数组
【发布时间】:2018-05-04 18:00:36
【问题描述】:

我知道这很简单,但我无法解决这个问题。请调查一下。

我有一个名为notification_updates 的表,它的数组是这样的:

Array
(
    [0] => common\models\NotificationUpdates Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 1
            [title] => This is the notification to inform you about this.
            [status] => 1
            [created_at] => 2017-11-20 08:29:21
        ) 
    )
    [1] => common\models\NotificationUpdates Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 2
            [title] => This is the notification to inform you about this cricket match
            [status] => 1
            [created_at] => 2017-11-20 06:24:09
        ) 
    )
    [2] => common\models\NotificationUpdates Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 3
            [title] => Inform you about this cricket match
            [status] => 1
            [created_at] => 2017-11-21 11:40:31
        )
    )
)

现在我还有 1 个表,其中第一个表的 primary_key (id) 在表 deleted_nofitication 中被称为 notification_id

这个表也有这样的数组:

Array
(
    [0] => common\models\DeletedNofitication Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [notification_id] => 1
        )
    )
    [1] => common\models\DeletedNofitication Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [notification_id] => 2
        ) 
    )
)

现在我必须对照 user_id 检查天气 notification_updates 表是否有此值。如果它在那里,那么它不应该在JSON 下显示通知。

我已经在 PHP (YII2) 中完成了这样的操作 - 请检查一下

$notifications = NotificationUpdates::find()->where([])->all();
$outt = [];  

foreach($notifications as $notification) {
    $deleted_notification = DeletedNofitication::find()
                                ->select('notification_id')
                                ->where(['user_id'=>$user_id])
                                ->all();  

    $outt[] = [
        'id' => $notification->id,
        'notification_title' => $notification->title
    ]; 
}

$out = [
    'notification'=> $outt,
    'success'     => true,
    'message'     => 'All Notification Updates'
]; 

【问题讨论】:

    标签: php arrays json arraylist yii2-advanced-app


    【解决方案1】:

    编辑:

    好的,既然我明白了你想要做的事情,我也许可以提供帮助。我对 YII2 不满意(即我从未使用过它),但您的代码可能看起来像这样。底线是我希望 SQL 只返回相关记录,这样我们就不必用我们的 php 做那个逻辑:

    <?php
    // We're trying to select all notifications except the ones that have already been shown.
    // Following query uses subquery, join would be better performancewise.
    // $notificationsSQL = "SELECT id,title FROM NotificationUpdates WHERE id NOT in (SELECT id FROM DeletedNotifications WHERE user_id = $user_id)";
    
    $notificationsAlreadyShown =  DeletedNofitication::find()->where(['user_id' => $user_id]);
    $notifications = NotificationUpdates::find()->where(['not in', 'id', $notificationsAlreadyShown]);
    
    if ($notifications->count() === 0){ //Or whatever method you use to count the results.
        return; // Or whatever you need to do to handle this case.
    }
    
    $outt = [];
    foreach ($notifications->all() as $notification) {
        $outt[] = [
            'id' => $notification->id,
            'notification_title' => $notification->title
        ];
    }
    
    $out = [
        'notification' => $outt,
        'success' => true,
        'message' => 'All Notification Updates'
    ]; 
    

    P.s 我在您的已删除通知中没有看到 user_id 列,可能需要检查一下。


    旧答案:

    抱歉,我不太清楚你的问题。 IE。我不明白你想要做什么。是不是只显示没有被删除的通知?

    首先引起我注意的是,您在第二个查询的 where 子句中有一个列 user_id,但我在表结构中看不到它 .我对 YII2 不太熟悉,但是您是否正在尝试做一些类似的事情:

    <?php
    $notifications = NotificationUpdates::find()->where([])->all();
    $outt = [];
    
    foreach ($notifications as $notification) {
        $deleted_notification = DeletedNofitication::find()
            ->select('notification_id')
            ->where(['notification_id' => $notification->id])
            ->all();
    
        // If the notification has been deleted, skip to next iteration.
        if (count($deleted_notification) > 0){
            continue;
        }
    
        $outt[] = [
            'id' => $notification->id,
            'notification_title' => $notification->title
        ];
    }
    
    $out = [
        'notification' => $outt,
        'success' => true,
        'message' => 'All Notification Updates'
    ]; 
    

    虽然这是您尝试做的事情,但您可能应该返回查询生成器并仅选择未删除的通知。或者更好的是,使用 YII2s 软删除(如果有的话)。

    【讨论】:

    • 我已经删除了deleted_nofitication 中的通知。现在我在一张表notification_updates 中有所有通知。我想用user_id 获取deleted_nofitication 中不存在的通知。如果 id=1 存在于已删除的通知中,且 user_id=1。然后在 notification_updates 数组中我不应该收到该通知。
    【解决方案2】:

    这个查询完成了我需要的一切..

     $sql = "select * from notification_updates where NOT EXISTS 
                 (select * from deleted_nofitication where notification_id = notification_updates.id AND user_id = ".$user_id." )" ; 
                $command = Yii::$app->db->createCommand($sql);
                $notifications = $command->queryAll();
    

    并从 $notifications 中获取值并将其添加到 json。

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多