ttmt_101 Posted July 10, 2013 Share Posted July 10, 2013 Hi all I'm using Wordpress, I have three simple pages with the titles - Page One, Page Two, Page Three Each page has a tag of 'content'. For each page I would like to create an array with a name the same as that page so Page_One[], Page_Two[] etc. I would like to then add and retrieve vales from this array. In the example here the is_Array echo says it's and array. The print_r and count show the array is empty. Is it possible to create an associative array and name it dynamically here, then add and retrieve values. <?php $mt_test_args = array( 'post_type' => 'page', 'tag' => 'font', 'order' => 'ASC' ); $mt_test_loop = new WP_Query($mt_test_args); if($mt_test_loop->have_posts()): while($mt_test_loop->have_posts()): $mt_test_loop->the_post(); $weight = get_post_meta($post->ID,'weight',false); // create array for each font $arr_name = get_the_title(); $arr_name = str_replace(' ','_',$arr_name); ${$arr_name} = array();// The font array echo is_Array($$arr_name) ? 'yes' : 'no'; // Add text and font size keys to array $$arr_name[$text] = 'first text'; $$arr_name[$font_size] = '2'; ?> <option><?php the_title(); ?></option> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?> <?php print_r($Alber_New); echo count($Alber_New); echo $Alber_New[$text]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280033-dynamically-create-associative-array/ Share on other sites More sharing options...
AbraCadaver Posted July 10, 2013 Share Posted July 10, 2013 (edited) You're using arrays so why use dynamic variables? Also, I don't see where you define $text and $font_size. $title = str_replace(' ','_',get_the_title()); //$pages[$title][$text] = 'first text'; //$pages[$title][$font_size] = '2'; // I think you meant: $pages[$title]['text'] = 'first text'; $pages[$title]['font_size'] = '2'; Edited July 10, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280033-dynamically-create-associative-array/#findComment-1440166 Share on other sites More sharing options...
AbraCadaver Posted July 10, 2013 Share Posted July 10, 2013 And really, since you're using the title as an array key you don't need the str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/280033-dynamically-create-associative-array/#findComment-1440167 Share on other sites More sharing options...
ttmt_101 Posted July 10, 2013 Author Share Posted July 10, 2013 Sorry, $text and $font_size is a typo, text and font_size were just examples of content i might add to the array, I'm not apply to edit the original post now. I want to use dynamic variables so I can l reference the arrays later in my code. If there are 3 pages and 3 arrays are created how do I reference the correct array. I used str_replace() because the page titles have spaces and I didn't think i could have space in an array name. Quote Link to comment https://forums.phpfreaks.com/topic/280033-dynamically-create-associative-array/#findComment-1440169 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.