I want to insert an array into a post meta, but something strange is happening. What am I missing here?
How I insert meta to a post:
// 1st Quarter
for ($month = 1; $month <= 3; $month++) {
$last_day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$first_date = strtotime("$year-$month-$first_day 00:00:00");
$last_date = strtotime("$year-$month-$last_day 23:59:59");
$month_dates = array(
'name' => 'P' . $month . '-FY' . $year,
'start_date' => date('Y-m-d', $first_date),
'end_date' => date('Y-m-d', $last_date),
'closed' => 0
);
array_push( $tmp_period, $month_dates );
} // end for
$quarter_name = 'Q1-FY' . $year;
add_post_meta( $post_id, '_q1_name', $quarter_name, true );
add_post_meta( $post_id, '_q1_subs', $tmp_period, false );
How I check the inserted meta:
$subs = get_post_meta( $post->ID );
echo '<pre>';
print_r( $subs, false );
echo '</pre>';
What the inserted meta actually looks like:
Array ( [_q1_name] => Array ( [0] => Q1-FY2018 ) [_q1_subs] => Array ( [0] => a:3:{i:0;a:4:{s:4:"name";s:9:"P1-FY2018";s:10:"start_date";s:10:"2018-01-01";s:8:"end_date";s:10:"2018-01-31";s:6:"closed";i:0;}i:1;a:4:{s:4:"name";s:9:"P2-FY2018";s:10:"start_date";s:10:"2018-02-01";s:8:"end_date";s:10:"2018-02-28";s:6:"closed";i:0;}i:2;a:4:{s:4:"name";s:9:"P3-FY2018";s:10:"start_date";s:10:"2018-03-01";s:8:"end_date";s:10:"2018-03-31";s:6:"closed";i:0;}} ) )
What I originally expected the meta to look like:
Array ( [_q1_name] => Array ( [0] => Q1-FY2018 ) [_q1_subs] => Array ( [0] => Array ( 'name' => 'some name', 'start_date' => some start date', 'end_date' => some end date, 'closed' => some value ), [1] => Array ( 'name' => 'some name', 'start_date' => some start date', 'end_date' => some end date, 'closed' => some value ), [2] => Array ( 'name' => 'some name', 'start_date' => some start date', 'end_date' => some end date, 'closed' => some value ), ) )