代码之家  ›  专栏  ›  技术社区  ›  eComEvo

更新Laravel集合中的单个键值

  •  1
  • eComEvo  · 技术社区  · 7 年前

    我正在根据Facebook的API响应构建一个集合。

    $ads = new \Illuminate\Support\Collection;
    
    if (!$ads->has($insight[$key])) {
        $ads->put($insight[$key], [
            'ad_id'                             => $insight[AdsInsightsFields::AD_ID],
            'ad_name'                           => $insight[AdsInsightsFields::AD_NAME],
            'ctr'                               => (float)$insight[AdsInsightsFields::CTR],
            'spend'                             => (float)$insight[AdsInsightsFields::SPEND],
        ]);
    } else {
        // Increment spend value here.
    }
    

    如果这是一个数组,我会这样做:

    $ads[$insight[$key]]['spend'] += $insight[AdsInsightsFields::SPEND];
    

    3 回复  |  直到 7 年前
        1
  •  1
  •   eComEvo    7 年前

    为了解决这个问题,我编写了能够执行所需更新的宏。

    // Set a single value by dot notation key.
    Collection::macro('set', function ($key, $new) {
        $key = explode('.', $key);
        $primary_key = array_shift($key);
        $key = implode('.', $key);
        $current = $this->get($primary_key);
    
        if (!empty($key) && is_array($current)) {
            array_set($current, $key, $new);
        } else {
            $current = $new;
        }
    
        $this->put($primary_key, $current);
    });
    // Increment a single value by dot notation key.
    Collection::macro('increment', function ($key, $amount) {
        $key = explode('.', $key);
        $primary_key = array_shift($key);
        $key = implode('.', $key);
        $current = $this->get($primary_key);
    
        if (!empty($key) && is_array($current)) {
            $new = array_get($current, $key, 0);
            $new += $amount;
    
            array_set($current, $key, $new);
        } else {
            $current += $amount;
        }
    
        $this->put($primary_key, $current);
    });
    // Decrement a single value by dot notation key.
    Collection::macro('decrement', function ($key, $amount) {
        $key = explode('.', $key);
        $primary_key = array_shift($key);
        $key = implode('.', $key);
        $current = $this->get($primary_key);
    
        if (!empty($key) && is_array($current)) {
            $new = array_get($current, $key, 0);
            $new -= $amount;
    
            array_set($current, $key, $new);
        } else {
            $current -= $amount;
        }
    
        $this->put($primary_key, $current);
    });
    

    有了这个,我需要做的就是:

    $ads->increment($insight[$key] . '.spend', $insight[AdsInsightsFields::SPEND]);
    

    如果我只想设置一个值,不管键是否存在,我可以这样做:

    $ads->set($insight[$key] . '.spend', $insight[AdsInsightsFields::SPEND]);
    
        2
  •  0
  •   whmkr    7 年前
    $ads->{$insight[$key]}['spend'] +=  $insight[AdsInsightsFields::SPEND];
    

    它看起来有点奇怪,但是您需要作为对象属性访问第一部分 ->propertyName ,即从数组中获取的属性名称 $insight[$key] [spend] 最后。

        3
  •  0
  •   Tharaka Dilshan    7 年前

    首先,这是数组的集合(不是集合的集合)。

    你的问题是如何在集合中获得一个特定的键值对,

    现在,您只需按如下所示增加“花费”值。

    $ads->get($insight[$key])['spend'] += $insight[AdsInsightsFields::SPEND];
    

    注意

    如果您需要将整个内容作为集合,请更新您的代码以生成集合的集合,如下所示

    $ads->put($insight[$key],  collection([
        'add_id'                            => $insight[AdsInsightsFields::AD_ID],
        'ad_name'                           => $insight[AdsInsightsFields::AD_NAME],
        'ctr'                               => (float)$insight[AdsInsightsFields::CTR],
        'spend'                             => (float)$insight[AdsInsightsFields::SPEND],
    ]);
    

    $ads->get($insight[$key])->spend += $insight[AdsInsightsFields::SPEND];