这是一个简单的答案,几乎太容易了,搜索起来有点困难。。。
PHP Foreach:
<?php $position = get_post_meta(get_the_ID(), '_moon_draggable_values', false);
if ($position){
foreach ($position as $key => $value)
echo "{$key} => {$value}'n";
}
?>
这输出了0 => 233px 1 => 435px
,我所要做的就是选择索引并回显它,我尝试了类似echo $value[1]
的方法,希望回显435px,但没有成功,也尝试了$key
。
结论:试图获得特定数组索引值0,1是中仅有的两个索引(仅有两个数组)
解决方案:
<?php $position = get_post_meta(get_the_ID(), '_moon_draggable_values', false);
$top = $position[0];
$left = $position[1];
?>
<div style="left:<?php echo $left ?>; top: <?php echo $top?>; position: absolute;">
<?php echo htmlspecialchars_decode(get_post_meta ($post->ID, '_moon_sortable_content', true));?>
</div>
为了回答你的问题,我会这样做:
//Define the position array:
$position = array("left" => "235px",
"top" => "432px");
//Checking the position array:
if(is_array($position))
//Echo the information.
echo "Left: " . $position["left"] . ", Top: " . $position["top"];
至于你关于这是否被视为良好实践的问题,我个人不明白为什么不会。
如果出于某种原因,你没有定义指数本身的奢侈,请执行以下操作:
//Define the position array:
$position = array("235px", "432px");
//Checking the position array:
if(is_array($position))
//Echo the information.
echo "Left: " . $position[0] . ", Top: " . $position[1];