php Laravel 中的数组推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12685420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:01:57  来源:igfitidea点击:

Array Push in Laravel

phpmysqlarrayslaravel

提问by monk

I am trying to push new array item into existing array variable that has items from database. What I want to do is add a new item named 'Others' at the end of this array and display it as select drop down in view which consists of all the items from database and at the end of this select the 'Others' item that I manually added in my controller.

我正在尝试将新的数组项推送到具有数据库项的现有数组变量中。我想要做的是在此数组的末尾添加一个名为“其他”的新项目,并将其显示为视图中的选择下拉列表,其中包含数据库中的所有项目,并在此末尾选择“其他”项目我手动添加到我的控制器中。

Here is what I tried to do:

这是我尝试做的:

    $competition_all = Competition::all();
    $newCompete = array('name'=>'Others');
    array_push($competition_all, $newCompete);

    $this->competition_games = array('Competition');

    foreach ($competition_all as $competition_games) {
        $this->competition_games[$competition_games->name] = $competition_games->name;
    }

what it said is like this

它说的是这样的

Unhandled Exception

Message:

Trying to get property of non-object Location:

C:\xampp\htdocs\khelkheladi\khelkheladi\application\controllers\register.php on line 104

未处理的异常

信息:

试图获取非对象位置的属性:

C:\xampp\htdocs\khelkheladi\khelkheladi\application\controllers\register.php 第 104 行

In my database the Competition has this type of column structure

在我的数据库中,比赛具有这种类型的列结构

->id
->year
->place
->name
->created_at
->updated_at

in given order.

按照给定的顺序。

What I'm trying to do is without actually inserting an item in database, just statically show the others select item in select tag in view. How do I insert such new item without actually inserting it to database but to display in view only?

我想要做的是没有在数据库中实际插入一个项目,只是在视图中静态显示选择标签中的其他选择项目。如何插入这样的新项目而不实际将其插入数据库而是仅在视图中显示?

The output what i'm getting before by just retrieving database item is like this

我之前通过检索数据库项目得到的输出是这样的

<select>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
  <option value="4">Value 4</option>
</select> 

what I like to do is like this

我喜欢做的是这样的

<select>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
  <option value="4">Value 4</option>
  <option value="5">Others</option>
</select> 

采纳答案by LSerni

The "clean" way to do it would be to create an instance of Competitionwithout committing it to the database, and repeat once more your cycle with the extra instance.

“干净”的方法是创建一个实例Competition而不将其提交到数据库,并使用额外的实例再次重复您的循环。

However, here you appear to be just producing a list, so it ought to be enough to do a much quicker addition to the final list:

但是,在这里您似乎只是在生成一个列表,因此应该足以对最终列表进行更快的添加:

$competition_all = Competition::all();
$this->competition_games = array('Competition');

foreach ($competition_all as $competition_games) {
    $this->competition_games[$competition_games->name] = $competition_games->name;
}
$this->competition_games['name'] = 'Others';

回答by Tony

That's because you're adding a non-object to the last element of the array.

那是因为您要向数组的最后一个元素添加一个非对象。

Here i suppose you get an array of objects with the name property

在这里,我假设你得到了一个带有 name 属性的对象数组

$competition_all = Competition::all();

Here you add a key=>value pair to the last element of the objects array

在这里,您将 key=>value 对添加到 objects 数组的最后一个元素

$newCompete = array('name'=>'Others');
array_push($competition_all, $newCompete);

Here you walk through the array of objects and when it comes to the last element the "$competition_games->name" has no name property

在这里,您遍历对象数组,当涉及到最后一个元素时,“$competition_games->name”没有 name 属性

foreach ($competition_all as $competition_games) {
            $this->competition_games[$competition_games->name] = $competition_games->name;
        }

Try something like including a stdclass for it like :

尝试为它包含一个标准类,例如:

$newCompete = new StdClass();
$newCompete->name = 'Others';
array_push($competition_all, $newCompete);