显示数组内容

时间:2013-11-15 21:26:36

标签: php arrays wordpress

我有一个数组如下;

<?php  

foreach ($arr['sections'] as $key => $value) 
{
   return  $key;
}

?>

返回我想要放入以下

的值part1,part2,part3
get_template_part( 'content', '$key');

因此我得到一段代码来显示3行并且部分到位,但我无法弄清楚输出它的正确方法,我尝试了类似

的内容
<?php  

foreach ($arr['sections'] as $key => $value) 
{
   $output  = "get_template_part( 'content', '";
   $output .= $key;
   $output .= "');";

   return $output;

}

?>

但它没有用,任何建议或帮助都表示赞赏。

2 个答案:

答案 0 :(得分:3)

只需通过参数

调用每个键的函数
foreach ($arr['sections'] as $key => $value) 
{

get_template_part( 'content',$key);

}

或者如果你想要一些变量中的所有内容

$output='';
foreach ($arr['sections'] as $key => $value) 
{

$output.=get_template_part( 'content',$key);

}
echo $output; /* will have the content from all parts (part1, part2, part3) */

答案 1 :(得分:0)

你没有显示数组,所以我们不知道你想要准确传递什么,但是如何:

$output = call_user_func_array('get_template_part', $arr['sections']);

或者,无论出于何种原因,您实际上都希望通过“密钥”:

$output = call_user_func_array('get_template_part', array_keys($arr['sections']));

或者添加'内容':

array_unshift($arr['sections'], 'content');
$output = call_user_func_array('get_template_part', $arr['sections']);
相关问题