Php 带有数组的printf问题
因此,我在获取数组、循环值以及使用foreach语句输出时遇到了问题 代码Php 带有数组的printf问题,php,Php,因此,我在获取数组、循环值以及使用foreach语句输出时遇到了问题 代码 <?php $example = array("test" => "hello world" ); foreach ($example as $arr) { printf("<p>what do you have to say for yourself?</p><p>%s!</p>",$arr["test"]); } ?> 希望获得输出: 你
<?php
$example = array("test" => "hello world" );
foreach ($example as $arr) {
printf("<p>what do you have to say for yourself?</p><p>%s!</p>",$arr["test"]);
}
?>
希望获得输出:
你有什么要说的吗
你好,世界
取而代之的是获取
你有什么要说的吗
啊
为什么只有一个字符
任何帮助都会很好
谢谢您的foreach循环已经在遍历数组的值,因此不要使用键再次引用该值:
<?php
$example = array("test" => "hello world" );
foreach ($example as $key => $val) {
printf("<p>what do you have to say for yourself?</p><p>%s!</p>",$val);
}
?>
根据注释中的另一个示例,您将无法使用循环,因为位置非常具体。而是在没有循环的情况下专门引用每个值:
$example = array(
"first" => "Bob",
"last" => "Smith",
"address" => "123 Spruce st"
);
printf("<p>my name is %s %s and i live at %s</p>",
$example['first'],
$example['last'],
$example['address']
);
$example=array(
“第一”=>“鲍勃”,
“last”=>“Smith”,
“地址”=>“云杉街123号”
);
printf(“我的名字是%s%s,我住在%s”,
$example['first'],
$example['last'],
$example['address']
);
您的foreach循环已经在遍历数组的值,因此不要使用键再次引用该值:
<?php
$example = array("test" => "hello world" );
foreach ($example as $key => $val) {
printf("<p>what do you have to say for yourself?</p><p>%s!</p>",$val);
}
?>
根据注释中的另一个示例,您将无法使用循环,因为位置非常具体。而是在没有循环的情况下专门引用每个值:
$example = array(
"first" => "Bob",
"last" => "Smith",
"address" => "123 Spruce st"
);
printf("<p>my name is %s %s and i live at %s</p>",
$example['first'],
$example['last'],
$example['address']
);
$example=array(
“第一”=>“鲍勃”,
“last”=>“Smith”,
“地址”=>“云杉街123号”
);
printf(“我的名字是%s%s,我住在%s”,
$example['first'],
$example['last'],
$example['address']
);
在关联数组上循环会在每次迭代的$arr
中放入一个值。当您尝试索引到$arr时,实际上是在索引到一个字符串,因此是单个字符。在关联数组上循环是在每次迭代中,$arr
中放入一个值。当您尝试索引到$arr时,实际上是在索引到一个字符串,因此是单个字符。Foreach假设数组中有多个元素。如果不只是像echo$example['test']那样回显元素;不需要循环构造。如果有多个元素:
$example = array('text'=>"what do you have to say for yourself?",'test' => "hello world" );
print_r($example);
foreach ($example as $value)
printf("<p>%s!</p>",$value);
$example=array('text'=>“你自己要说什么?”,'test'=>“hello world”);
打印(示例);
foreach($value作为示例)
printf(“%s!”,$value);
foreach在每个循环上将数组元素的值赋给名为$value的变量。有意义吗?Foreach假设数组中有多个元素。如果不只是像echo$example['test']那样回显元素;不需要循环构造。如果有多个元素:
$example = array('text'=>"what do you have to say for yourself?",'test' => "hello world" );
print_r($example);
foreach ($example as $value)
printf("<p>%s!</p>",$value);
$example=array('text'=>“你自己要说什么?”,'test'=>“hello world”);
打印(示例);
foreach($value作为示例)
printf(“%s!”,$value);
foreach在每个循环上将数组元素的值赋给名为$value的变量。有意义吗?也许这样看会有帮助
<?php
$example = array("test" => "hello world", "foo" => "bar");
foreach ($example as $key => $val) {
# option 1
echo "<p>what do you have to say for yourself?</p><p>$key => $val</p>";
# option 2
echo "<p>what do you have to say for yourself?</p><p>$key => $example[$key]</p>";
}
?>
一旦看到它是如何迭代的,就可以将语句放回printf()中,或者对变量执行任何操作
请注意,如果您有多维数组,则可以通过寻址键来引用数组的下一级 也许这样看会有所帮助
<?php
$example = array("test" => "hello world", "foo" => "bar");
foreach ($example as $key => $val) {
# option 1
echo "<p>what do you have to say for yourself?</p><p>$key => $val</p>";
# option 2
echo "<p>what do you have to say for yourself?</p><p>$key => $example[$key]</p>";
}
?>
一旦看到它是如何迭代的,就可以将语句放回printf()中,或者对变量执行任何操作
请注意,如果您有多维数组,则可以通过寻址键来引用数组的下一级 太棒了,但是如果数组中有多个key=>值呢?@danferth:foreach将遍历数组中的每个key/value对,并为每个key/value对运行printf。@MattS
printf(你有什么要说的吗?%s%s,$val)代码>给了我一些错误。该示例是一个简化版本。事实上,这将是一个庞大的接触阵列。在这个项目中,数据库是不可能的。@danferth:那么你必须提供一个更详细的例子,因为这个例子没有错误。除非内存不足,否则数组的大小无关紧要。@MattS$example=array(“first”=>“Bob”、“last”=>“Smith”、“address”=>“123 struce st”);foreach($key=>$val){printf(“我的名字是%s%s,我住在%s”,$val);}
太棒了,但是如果我在数组中有多个key=>值怎么办?@danferth:foreach将遍历数组中的每个key/值对,并为每个key/值对运行printf。@MattSprintf(“你有什么要说的吗?%s%s”,$val)
给了我一些错误。这个例子是一个简化的版本。实际上,它是一个用于联系人的大型数组。在这个项目中,数据库是不可能的。@danferth:那么你必须提供一个更详细的例子,因为这个例子没有错误。数组的大小是无关紧要的,除非你的内存用完了ory@MattS$example=array(“first”=>“Bob”,“last”=>“Smith”,“address”=>“123 struce st”);foreach($key=>$val){printf(我的名字是%s%s,我住在%s,$val);}