在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

duanquanhan2333
duanquanhan2333 我应该说查找访问我的redis密钥是:key:$shortUrl:visits...所以我需要在这些数组中返回的值来查找访问....也许我想要存储这个全部错误以及
3 年多之前 回复

1个回答

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');
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐