Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a metabox with an array of input:

<input type="text" name="activite_dates[]" size="30" /><br />
<input type="text" name="activite_dates[]" size="30" /><br />
<input type="text" name="activite_dates[]" size="30" /><br />
...

And i'm saving it like this

update_post_meta( $post_id, "dates", $_POST['activite_dates'] );

The problem is that when i'm retrieving it I have to make a ugly function to read multiple dimension array

Array 
(
[0] => Array
    (
        [0] => DATA1
        [1] => DATA2
        [2] => DATA3
        [3] => DATA99
    )

[1] => Array
    (
        [0] => DATA1
        [1] => DATA2
        [2] => DATA3
        [3] => DATA99
    )

[2] => Array
    (
        [0] => DATA1
        [1] => DATA2
        [2] => DATA3
        [3] => DATA99
    )

)

And when I'm saving it, it's only add to append to existent array

Array 
(
[0] => Array
    (
        [0] => DATA1
        [1] => DATA2
        [2] => DATA3
        [3] => DATA99
        [4] => DATA1
        [5] => DATA2
        [6] => DATA3
        [7] => DATA99
    )

[1] => Array
    (
        [0] => DATA1
        [1] => DATA2
        [2] => DATA3
        [3] => DATA99
        [4] => DATA1
        [5] => DATA2
        [6] => DATA3
        [7] => DATA99        
     )

[2] => Array
    (
        [0] => DATA1
        [1] => DATA2
        [2] => DATA3
        [3] => DATA99
        [4] => DATA1
        [5] => DATA2
        [6] => DATA3
        [7] => DATA99
    )

)

So first, why do I have 3 value at the first level then my array is multiplied inside of the second level ?

For information here is my code for the view :

$dates_gf = get_post_meta( $object->ID, 'dates', false );
foreach ($dates_gf as $date_gf) {
    if (is_array($date_gf)){
        foreach ($date_gf as $arr_date_gf) {
            ?> <input type="text" name="activite_dates_gf[]" value="<?php echo $arr_date_gf; ?>" size="30" /><br /> <?php
        }
    } else {
        ?> <input type="text" name="activite_dates_gf[]" value="<?php echo $date_gf; ?>" size="30" /><br /> <?php
    }
} ?>

Thank you very much !

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
<?php
// Check to make sure array is ready
if( isset($_POST['activite_dates']) && is_array($_POST['activite_dates']) ) {
    // If we have an array ready to save delete all the previous data
    delete_post_meta( $post_id, "dates");
    foreach($_POST['activite_dates'] as $date) {
        // Use add post meta instead of passing the array directly in
        add_post_meta( $post_id, "dates", esc_attr($date));
    }
}



$dates_gf = get_post_meta( $object->ID, 'dates', false );

// No need for the is_array() check when using false for the $single variable. Check for empty() instead
if(!empty($dates_gf)) {
    foreach($dates_gf as $date_gf) {
        ?> <input type="text" name="activite_dates_gf[]" value="<?php echo $date_gf; ?>" size="30" /><br /> <?php
    }
} ?>
share|improve this answer
    
Thank you Cole, that's worked like a charm ! I find my self stupid to haven't think this way at first ! –  Jonathan Lafleur Feb 18 '13 at 18:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.