比较 php 数组中的值 - 如果不存在则将其添加到 json 数组

Compare a value in array in php - if not exist then add that to json array

我知道这很简单,但我无法解决这个问题。请查看此内容。

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

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 个 table,其中第一个 table 的 primary_key (id) 在 table 中被称为 notification_id deleted_nofitication.

这个table也有这样的数组:

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
        ) 
    )
)

现在我必须检查天气 notification_updates table 将此值与 user_id 进行比较。如果它在那里那么不应该在 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'
]; 

编辑:

好的,既然我明白了你想要做什么,我也许可以提供帮助。我不喜欢 YII2(即我从未使用过它)table,但您的代码可能看起来像这样。最重要的是,我希望 SQL 仅 return 相关记录,这样我们就不必使用 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,但我没有在table 结构。我对 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'
]; 

虽然如果这就是您想要做的,您可能应该返回到您的查询生成器,并且只 select 未删除的通知。或者更好的是,使用 YII2s 软删除(如果有的话)。

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

 $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。