1

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 !

1
<?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
    }
} ?>
1
  • Thank you Cole, that's worked like a charm ! I find my self stupid to haven't think this way at first ! Feb 18 '13 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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