Jump to content

Recommended Posts

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];
?>


Link to comment
https://forums.phpfreaks.com/topic/280033-dynamically-create-associative-array/
Share on other sites

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 by AbraCadaver

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.