Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a page set up like Page One, Page Two, Page Three

Each page has a tag of 'content'.

In the Wordpress loop I would like to create an array for each page with the page title as the array name.

I would then like to add and get values from the array using the array name.

I have some demo code here.

If I try to get the length of the array using the array name it returns '0' and the keys/values are not returned.

    <?php
        $mt_test_args = array(
            'post_type' => 'page',
            'tag' => 'content',
            '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();  

        // create array for each page   
        $arr_name = get_the_title();
        $arr_name = str_replace(' ','_',$arr_name);
        $$arr_name = array();// The page array

        // Add text and color keys to array
        $$arr_name[$text] = 'Page Text';
        $$arr_name[$color] = 'red';

    ?>  

    <option><?php the_title(); ?></option>

    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>

    <?php 
        print_r($Page_One);
        echo count($Page_One); // no echo
        echo $Page_One[$text]; // no value
    ?>
share|improve this question
    
I have answered this, but I believe the question to be off-topic here, since at it's core it's about variable variables and/or a code review unrelated to WordPress. Voted to close. –  Johannes Pille Jul 9 '13 at 10:54
    
The first part of this question was answered but it hasn't worked to achieve the second part, setting a using key/vales in the array using the name of the array –  Simon Cooper Jul 9 '13 at 11:08
add comment

1 Answer

From the PHP manual:

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Hence before diving into your code further:

  1. It would need much more than $arr_name = str_replace(' ','_',get_the_title()); to make this fail-safe. Currently, your code is bound to fail as soon as the page title contains any non-alphanumeric characters.

  2. Because of the above, among other reasons, I'd strongly suggest you reconsider the general architectural approach. Ask yourself this: Why would you need variable variable in the first place? (I don't see a reason and I doubt this to be the most viable solution, even if a valid reason exists.)

share|improve this answer
    
I don't think there would be a page title with non-alphanumeric characters. Can you suggest a better way to do this, I would just like to create an array using the page title, any help would be greatly appreciated. –  Simon Cooper Jul 9 '13 at 11:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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