【问题标题】:Adding values to array in a loop在循环中向数组添加值
【发布时间】:2019-09-20 03:52:18
【问题描述】:

我正在开发一个 laravel 项目,该项目在满足某些条件时将值存储到循环中的数据库条目。

如果条目是第一次,这将首先创建一个数组并向其添加一个值。此后,它会调用该数组并继续向其添加值。

if(is_null($lead->shown_to)) {
    $a = array();
    array_push($a, "lead 1");
    $lead->shown_to = serialize($cart);
    $lead->save();
} else {
    $a=unserialize($lead->shown_to);
    array_push($a, "lead 2");
    $lead->shown_to = serialize($a);
    $lead->save();
}

能够创建一个数组并向其重复添加不同的元素。

有没有办法首先检查元素是否存在于其中。如果是,就继续前进,否则添加它?

提前致谢。

【问题讨论】:

  • 你解决了吗?
  • 是的。谢谢你的提问。第二部分有问题。 else{ $a=unserialize($lead->shown_to); if(!in_array(auth()->user()->id, $a)) { $a[] = auth()->user()->id; $lead->shown_to = serialize($a); $铅->保存(); $user= Wallet::where('user_id','=', auth()->user()->id)->decrement('balance', 0); } }
  • 为未来的访问者发布解决方案会很好。

标签: php arrays laravel eloquent


【解决方案1】:

您可以使用几种方法。

如果存在,您可以先使用数据库中的列在数据库中查找值,例如:

$result = Model::where( 'column', 'value' );

if ( $result ) {
  // update already exists
} else {
  // create one
}


// Retrieve flight by name, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve by name, or instantiate...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

这也取决于你在寻找什么,因为firstOrCreate 将值保存到数据库中,firstOrNew 只是创建一个新实例,你需要在其中调用save()

【讨论】:

  • 谢谢。主要问题是将它存储为一个数组,然后下次引入同一个数组以向其中添加新元素。
【解决方案2】:

要检查数组中是否存在值,您可以使用array_search()。如果存在,这将返回该值。如果不是,则返回 false

if(!array_search('lead 2', $a)) {
    // array does't has 'lead 2' so,
    array_push('lead 2', $a);
}

【讨论】:

    【解决方案3】:

    在 Laravel 中,我会利用 Collections,因为它们有很多有用的方法可以使用。

    我会这样做:

    选项 1

    //Depending on the value of $lead->show, initialize the cart variable with the serialization of the attribute or and empty array and transform it to a collection.
    
    $cart = collect($lead->shown_to ? unserialize($lead->shown_to) : []);
    
    //Ask if the collection doesn't have the given value. If so, added it.
    if (!$cart->contains($your_value)) {
        $cart->push($your_value);
    }
    
    //Convert to array, serialize and store
    $lead->shown_to = serialize($cart->toArray());
    $lead->save();
    

    选项 2

    //Depending on the value of $lead->show, initialize the cart variable with the serialization of the attribute or and empty array and transform it to a collection.
    
    $cart = collect($lead->shown_to ? unserialize($lead->shown_to) : []);
    
    //Always push the value
    $cart->push($your_value);
    
    //Get the unique values, convert to an array, serialize and store
    $lead->shown_to = serialize($cart->unique()->toArray());
    $lead->save();
    

    您可以使用这些集合获得更多创意,并且它们在 Laravel 上阅读效果更好

    【讨论】:

      【解决方案4】:

      我认为你可以使用 updateOrCreate,如果不存在,它会立即创建,如果存在,它会更新它,所以你可以继续为 shown_to 属性赋值

      $lead= App\Lead::updateOrCreate(
          ['name' => 'Lead 1'],
          ['shown_to' => serialize($a)]
      );
      

      如果你想保留现有的showed_to更好地使用json数据,那么你可以这样做

      $lead= App\Lead::updateOrCreate(
              ['name' => 'Lead 1'],
              ['shown_to' => json_encode(array_push(json_decode($a), $newData))]
          );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-25
        • 2011-07-20
        • 1970-01-01
        • 2018-06-30
        • 2014-04-16
        • 1970-01-01
        • 2022-01-15
        相关资源
        最近更新 更多