PHP Multidimensional array within nested foreach loop -
working on wordpress metabox-plugin , trying save metadata multidimensional array (in text explode later on).
this code:
function wordpress_metabox() { $event_adress = explode('|', get_post_meta($post->id, '_event_adress', true)); foreach ($event_id $key => $id){ echo '<div class="event"> <div class="ticket"> <input type="text" name="_event_adress[]" value="'.$event_adress[$key].'" /> <input type="text" name="_event_price[]" value="'.$event_price[$key].'" /> </div><!-- end .ticket --> <div class="ticket"> <input type="text" name="_event_adress[]" value="'.$event_adress[$key].'" /> <input type="text" name="_event_price[]" value="'.$event_price[$key].'" /> </div><!-- end .ticket --> <input type="button" class="button" id="add_ticket" value="+ add ticket" /> </div><!-- end .event --> <input type="button" class="button" id="add_event" value="+ add event" />'; } } //this save function function wordpress_metabox_save($post_id, $post) { // user allowed edit post or page? if ( !current_user_can( 'edit_post', $post->id )) return $post->id; // ok, we're authenticated: need find , save data // we'll put array make easier loop though. $event_meta['_event_adress'] = $_post['_event_adress']; $event_meta['_event_id'] = $_post['_event_id']; // add values of $event_meta custom fields foreach ($event_meta $key => $value) { // cycle through $event_meta array! if( $post->post_type == 'revision' ) return; // don't store custom data twice $value = implode('|', (array)$value); // if $value array, separate | if(get_post_meta($post->id, $key, false)) { // if custom field has value update_post_meta($post->id, $key, $value); } else { // if custom field doesn't have value add_post_meta($post->id, $key, $value); } if(!$value) delete_post_meta($post->id, $key); // delete if blank } }
the should foreach-loop haven't gotten far yet that's why wrote same div twice illustrate. can please answer how can save each ticket adress , price in multidimensional array? i'm not sure if it's possible type e.g. _event_adress[][] store each data within nested array? or have type first key in?
Comments
Post a Comment