doulutian4843 2017-03-11 10:02
浏览 95

在foreach循环中间返回redis值?

Wondering if it is even possible to return values from my redis cache while i am performing a PHP foreach loop?

I might be over thinking this...

essentially i have a MySql instance and a redis instance.

my MySql instance returns the following:

array:5 [▼
  0 => array:6 [▼
    "user_id" => 2
    "short_url_key" => "aQpjoM"
    "original_url" => "www.domain.com/"
    "deleted_at" => null
    "created_at" => "2017-03-11 08:19:02"
    "updated_at" => "2017-03-11 08:19:02"
  ]
  1 => array:6 [▼
    "user_id" => 2
    "short_url_key" => "olW7uN"
    "original_url" => "www.domain.com/products/"
    "deleted_at" => null
    "created_at" => "2017-03-11 09:05:23"
    "updated_at" => "2017-03-11 09:05:23"
  ]
  2 => array:6 [▼
    "user_id" => 2
    "short_url_key" => "u4rjLA"
    "original_url" => "www.domain.com/products/asdf"
    "deleted_at" => null
    "created_at" => "2017-03-11 09:05:56"
    "updated_at" => "2017-03-11 09:05:56"
  ]
  3 => array:6 [▼
    "user_id" => 2
    "short_url_key" => "fOuOju"
    "original_url" => "www.domain.com/"
    "deleted_at" => null
    "created_at" => "2017-03-11 09:06:30"
    "updated_at" => "2017-03-11 09:06:30"
  ]
  4 => array:6 [▼
    "user_id" => 2
    "short_url_key" => "XmSBTK"
    "original_url" => "www.domain.com/xyz"
    "deleted_at" => null
    "created_at" => "2017-03-11 09:18:54"
    "updated_at" => "2017-03-11 09:18:54"
  ]
]

Now My Redis instance stores the short_url_key, the original_url & I store the visits their as well (which is what i am trying to return along with this data) The visits just incriments on my redis key but would love to return this data along as well... Trying to think of how i can do that as part of my PHP loop? Or is their something I am not thinking of?

ideally I would like to append each of these arrays with the visits count from my redis instance.

"visits" => 22

update: I am able to loop through and return all the values... but now i am not sure how to merge that into the other array..

    foreach ($datas as $data)
    {
        echo Cache::get('short:' . $data['short_url_key'] . ':visits');
    }
    exit();

Cheers

Citti

  • 写回答

1条回答 默认 最新

  • dousonghs58612 2017-03-11 10:59
    关注

    ok I was clearly overthinking this - I ended up appending to the array like below. Worked like a charm. cheers.

        foreach ($data as $key => $value)
        {   
            // will switch short_url_key to visits when i know they line up correctly
            $data[$key][$value['short_url_key']] = Cache::get('short:' . $value['short_url_key'] . ':visits');
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?