目前我将我的一个函数传递给这样的数组
$inputs = array("FOOD" => "Pancake");
然后在我的函数中我做了类似下面的事情
foreach ($inputs as $label => $data)
{
echo ($label . $data);
}
以上只是我刚刚写的一个例子,所以我希望它是正确的。无论如何,具有 foreach 循环的函数也包含以下内容
$this->SetFillColor(190,205,44);
现在对于每个循环,我还想设置输出的填充颜色。所以我正在考虑做类似下面的事情
$inputs = array("FOOD" => array("Pancake", '190,205,44'));
如果我 var_dump 传递我的函数的数组,我看到了
array:1 [▼
"FOOD" => array:2 [▼
0 => "PANCAKE"
1 => "190,205,44"
]
]
所以这一切似乎都是正确的。现在在我的职能中我做
foreach($inputs as $label => $data){
foreach($data as $content => $colour) {
$this->SetFillColor($colour);
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $content, 0, 0, 'L', 1, 0);
}
}
现在我有几个问题。首先,它似乎输出两行。第一个单元格打印出 FOOD 就像它应该做的那样,但第二个单元格打印出 0(应该是 PANCAKE)。第二行再次打印出 FOOD,但第二个单元格打印出 1。
那么我怎样才能让它只打印出第一行呢?另一个问题是 $colour,因为这是一个字符串,但我认为它需要是一个 int。
感谢任何建议。
谢谢
最佳答案
试试这个:
<?php
foreach($inputs as $label => $data){
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
// content is on index 0
$this->Cell(100, 7, $data[0], 0, 0, 'L', 1, 0);
// color on index 1
$this->SetFillColor($data[1]);
}
或者甚至更好地创建一个\stdClass 对象,如下所示:
<?php
$foo[0]['food'] = new \stdClass;
$foo[0]['food']->content= 'Pancake';
$foo[0]['food']->color = '190,205,44';
$foo[1]['food'] = new \stdClass;
$foo[1]['food']->content= 'Apple';
$foo[1]['food']->color = '255,0,0';
for($i=0,$cnt=count($foo);$i<$cnt;$i++) {
foreach($foo[$i] as $label => $data) {
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $data->content, 0, 0, 'L', 1, 0);
$this->SetFillColor($data->color);
}
}
编辑: 您也可以这样做:
<?php
$foo[0]['food']->color = new \stdClass;
$foo[0]['food']->content= 'Pancake';
$foo[0]['food']->color->r = 190;
$foo[0]['food']->color->g = 205;
$foo[0]['food']->color->b = 44;
... [剪断]
$c = $data->color;
$this->SetFillColor($c->r,$c->g,$c->b);
或者甚至做更多的 OOP 风格
<?php
class color {
private $r;
private $g;
private $b;
public function get_r() {
return (float) $this->r;
}
public function get_g() {
return (float) $this->g;
}
public function get_b() {
return (float) $this->b;
}
public function set_r($r) {
$this->r = $r;
return $this;
}
public function set_g($g) {
$this->g = $g;
return $this;
}
public function set_b($b) {
$this->b = $b;
return $this;
}
static public function factory() {
return new color;
}
public function __construct() {
}
}
class food {
private $content;
private $color;
public function get_content() {
return $this->content;
}
public function get_color() {
return $this->color;
}
public function set_content($content) {
$this->content = $content;
return $this;
}
public function set_color(color $color) {
$this->color = $color;
return $this;
}
static public function factory() {
return new food;
}
public function __construct() {
}
}
$color = color::factory()->set_r(255)->set_g(0)->set_b(0);
$dataarray[] = food::factory()->set_content('Apple')->set_color($color);
foreach($dataarray as $food) {
echo $food->get_content();
echo $food->get_color()->get_r();
echo $food->get_color()->get_g();
echo $food->get_color()->get_b();
}
关于带有整数的 PHP 多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581502/