0

This is an easy answer, almost too easy that it makes searching for it kinda of hard...

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";  
            }                             
?>

This outputs 0 => 233px 1 => 435px all Im trying to do is, select the index and echo it, I tried something like echo $value[1] hoping to echo the 435px, that didnt work, also trying with $key.

Conclusion: Trying to get a specific array index value 0,1 are the only two indexes (only two arrays)

Solution:

<?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>
4

1 回答 1

1

为了回答你的问题,我会这样做:

    //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];
于 2013-07-21T09:09:09.810 回答