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 に答える