【问题标题】:Foreach in array for codeigniter用于codeigniter的数组中的foreach
【发布时间】:2013-12-30 02:53:54
【问题描述】:

所以,我正在使用 codeigniter,我正在尝试使用日历类并将“事件”传递给我的日历。这是由一个数组和一个标识符来完成的,如下所示:

$calendar_info = array(
  'month' => '12',
  'year' => '2013',
  'data' => array(
    3  => 'Spaghetti<br/>Meatballs<br/>Salad<br/>Canned Fruit',
    4  => 'Ham<br/>Mashed Potatoes<br/>Corn<br/>Salad</br>'
   )
);

现在我无法将菜单项硬编码到应用程序中,而是从数据库中获取它们。我的问题在于将 foreach 放入数组中,出现以下错误:

Parse error: syntax error, unexpected T_FOREACH, expecting ')'

这个错误只是一部分,所以不要透露我的网址。以下是我现在正在尝试做的事情并得到上面的错误。

    $this->db->select('*');
    $this->db->from('menus');
    $this->db->where('user_id', $session_data['id']);

    $query = $this->db->get();

    $calendar_info = array(
        'month' => '12',
        'year' => '2013',
        'data' => array(
            foreach ($query->result() as $row)
            {
                $row->menu_day => $row->menu_entree.'<br/>'.$row->menu_sideone.'<br/>'.$row->menusidetwo.'<br/>'.$row->menudessert
            }
        ),
    );

更新

这正是我在下面拥有/正在尝试做的事情:

我有一个数据库表,其中包含我每月每一天的菜单项。

这些列是:

menu_day
menu_month
menu_entree
menu_sideone
menu_sidetwo
menu_dessert

数组需要如下所示:

array(
  menu_day => 'menu_entree<br/>menu_sideone<br/>menu_sidetwo<br/>menu_dessert
)

或者例如:

array(
  3 => 'Spaghetti<br/>Meatballs<br/>Salad<br/>Canned Fruit'
)

【问题讨论】:

  • 你不能在数组定义中使用foreach。你到底想做什么?数组数据应该怎么看?
  • 我更新了我的问题以便更好地解释。

标签: php codeigniter


【解决方案1】:

您不能像您尝试做的那样在array() 声明中循环。相反,正确的做法是使用循环构建数组,然后将其分配$calendar_info['data']

不要将 PHP 代码视为动态构建的东西。这是您正在生成的数组数据结构,而不是定义它的代码。

// First build the array with your loop...
// An empty array to hold the result...
$data = array();
foreach ($query->result() as $row)
{
  // Add array keys to the array while looping...
  $data[$row->menu_day] = $row->menu_entree.'<br/>'.$row->menu_sideone.'<br/>'.$row->menusidetwo.'<br/>'.$row->menudessert;
}

$calendar_info = array(
    'month' => '12',
    'year' => '2013',
    // Then assign it into the appropriate place...
    'data' => $data
);

您不需要构建变量$data 只是为了稍后扔掉。您也可以先在$calendar_info['data'] 中使用空数组定义$calendar_info,然后在foreach 循环中将键添加为$calendar_info['data'][$row-&gt;menu_day]

【讨论】:

  • 我收到一条错误消息,指出 unexpected T_DOUBLE_ARROW 我猜这是因为 for each 中的 =&gt;,我们只需要执行以下操作吗?:"=&gt;".跨度>
  • 抱歉,应该是=,而不是=&gt;
  • 哦,好吧。我不确定。
【解决方案2】:

您根本无法将foreach 放入数组中,就像您提出问题的方式一样。您需要使用 foreach 循环提前设置值,然后以这种方式将该数组放入 $calendar_info

$this->db->select('*');
$this->db->from('menus');
$this->db->where('user_id', $session_data['id']);

$query = $this->db->get();

$data_array = array();
foreach ($query->result() as $row) {
  $data_array[$row->menu_day] = $row->menu_entree . '<br/>' . $row->menu_sideone . '<br/>' . $row->menusidetwo . '<br/>' . $row->menudessert;
}

$calendar_info = array(
    'month' => '12',
    'year' => '2013',
    'data' => $data_array
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-14
    • 2017-04-07
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多