【发布时间】:2017-11-04 11:18:27
【问题描述】:
我正在尝试将一个键编号为 0 到 9 的数组传递给我的视图。但是,当我尝试为所有键回显特定键或 foreach 以显示数据时,什么都没有显示。
我的控制器:
$i=0;
$cleanarray = array();
foreach ($phrase as $innerphrase) {
if (is_array($innerphrase)) {
foreach ($innerphrase as $value) {
$cleanarray[$i] = $value;
$i++;
}
}
}
$this->load->view('blogview', $cleanarray);
输出:
Array
(
[0] => On Friday evenings in Thailand, sandwiched between the evening news and a popular soap opera, is a prime-time programme that has been running for three years, or ever since the military took power in a May 22, 2014 coup.
[1] => The Reds pulled off their biggest comeback in a year to finally end their pesky losing streak.
[2] => A co-founder of Twitter says he's sorry if the popular social media platform helped put Donald Trump in the White House, as the president has suggested.
[3] => Daniel Suarez is thrilled to be competing in the NASCAR All-Star race after struggling in his rookie season.
[4] => Lexi Thompson remained in position for her first victory since a rules infraction cost her a major title, shooting a 2-under 69 in tricky wind conditions Saturday to take a three-stroke lead over In Gee Chun into the final round of the Kingsmill Championship.
[5] => Boston Celtics guard Isaiah Thomas will miss the remainder of the NBA playoffs after aggravating a hip injury during Game Two of the Eastern Conference final, the team announced on Saturday.
[6] => Yankees manager Joe Girardi has been ejected during an animated argument and New York pitching coach Larry Rothschild and Tampa Bay starter Matt Andriese also have been tossed in a testy game at Tropicana Field.
[7] => Ed Carpenter turned a tough draw into a winning hand Saturday.
[8] => Cloud Computing pulled off a major surprise in the Preakness Stakes in Maryland on Saturday, charging down the stretch to overtake Classic Empire and win the second race of U.S. thoroughbred racing's Triple Crown.
[9] => Sometimes it pays to have a fresh horse.
)
我的看法:
<html>
<head>
</head>
<body>
<h1>I am terrible at PHP!</h1>
<h2><?php echo $0; ?></h2>
</body>
</html>
但是什么也没显示。我也尝试过以同样的方式使用 print_r。我知道 CodeIgniter 不允许我在控制器中传递变量,而是允许我直接访问数组中的键。
最终我想遍历数组并显示所有结果,但我试图从“小”开始,至少让数组的单个元素传递到视图。如果有人能解释为什么这不起作用,以及如何使用 for 或 foreach 来遍历数组,我将不胜感激。
我知道这个问题可能已经回答过很多次了,但在我的一生中,我无法将这些问题的回答推断为适用于我的情况的解决方案。
【问题讨论】:
-
非常感谢所有回复的人!
标签: php arrays codeigniter foreach