在foreach循环中间返回redis值?

时间:2017-03-11 10:02:57

标签: php laravel foreach

想知道在我执行PHP foreach循环时是否甚至可以从我的redis缓存中返回值?

我可能在想这个......

基本上我有一个MySql实例和一个redis实例。

我的MySql实例返回以下内容:

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"
  ]
]

现在我的Redis实例存储了short_url_key,original_url&我也存储了他们的访问(这是我试图返回的数据) 访问只是在我的redis密钥上实现,但是也希望同时返回这些数据...试着想一想我如何能够做到这一点作为我的PHP循环的一部分?或者他们是我没想到的东西?

理想情况下,我想在每个数组中添加来自我的redis实例的访问次数。

"visits" => 22

更新:我能够循环并返回所有值...但现在我不知道如何将其合并到另一个数组中..

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

干杯

CITTI

1 个答案:

答案 0 :(得分:0)

好吧,我显然已经过度思考了这个问题 - 我最终会像下面那样追加到数组中。工作就像一个魅力。干杯。

    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');
    }