使用CodeIgniter将数组从控制器传递到视图

6ju8rftf  于 2022-12-07  发布在  其他
关注(0)|答案(5)|浏览(171)

我试图将一个键编号为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来遍历数组,我将非常感激。
我知道这个问题可能已经回答了很多次,但就我的一生而言,我不能将对这些问题的回答外推到一个适用于我的情况的解决方案中。

apeeds0o

apeeds0o1#

使用此代码:

控制器

$i=0;
$cleanarray = array();

foreach ($phrase as $innerphrase) {
    if (is_array($innerphrase)) {
        foreach ($innerphrase as $value) {
            $cleanarray[$i] = $value;
            $i++;
        }
    }
}

$array2 =  array();  // create a new array
$array2['contents']= $cleanarray; // add $cleanarray to the new array
$this->load->view('blogview', $array2); // pass the new array as the parameter

内部视图文件

echo(“内容:“);
//打印$cleanarray中的元素
foreach($内容作为$瓦尔)

echo "$val<br/>";

注意:此处无法访问$array2。请改用“$contents[]”。

f0brbegy

f0brbegy2#

在控制器中创建$cleanarray后,应用此

$data['cleanarray'] = $cleanarray;//assign $cleanarray to $data
$this->load->view('blogview', $data);//pass $data to your view

在访问阵列的视图中应用此

<h2><?php echo $cleanarray[0]; ?></h2>
vs3odd8k

vs3odd8k3#

据我所知,codeigniter不会将没有数组的数据从控制器传递到视图,所以你可以尝试下面的代码,我必须确保它的工作。
在控制器中

$i=0;
    $cleanarray = array();
    foreach ($phrase as $innerphrase) 
    {
         if (is_array($innerphrase)) 
         {
                foreach ($innerphrase as $value) 
                {
                        $cleanarray[$i] = $value;
                        $i++;
                }
         }
    }
    $data['cleanarray'] = $cleanarray
    $this->load->view('blogview', $data);

在你的视图中打印这样的数组print_r($cleanarray);,我相信这段代码会起作用。

7kjnsjlb

7kjnsjlb4#

在Codeigniter应用程序中将数据从控制器传递到视图时,$this->load->view()方法调用的第二个参数应该是关联数组。
在视图文件中,有效负载中的所有第一级键都被转换为变量名。这是在控制器数组上调用PHP的原生extract()函数所预期的行为。
您的程式码可以这样调整:
控制器:

$payload = [];
foreach ($phrase as $innerPhrase) {
    if (is_array($innerPhrase)) {
        foreach ($innerPhrase as $payload['cleanArray'][]);
    }
}

$this->load->view('blogview', $payload);

查看方式:

...
<?php
foreach ($cleanArray as $string) {
    echo "<h2>$string</h2>";
}

关于在索引数组上使用extract()的相关阅读:Using php extract function with numeric indexes
长期代码点火器维护@纳夫just says no

5ktev3wc

5ktev3wc5#

我的建议是:
我不建议你用数字作为变量名。要解决这个问题,你可以让它的名字以'_'开头。例如:$_1 .
因此,在循环中,将$cleanarray[$i] = $value;更改为$cleanarray['_'.$i] = $value;
当然,您需要在视图echo $_0;上调用它

相关问题