0

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
    ?>
2
  • 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. Commented Jul 9, 2013 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 Commented Jul 9, 2013 at 11:08

1 Answer 1

0

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.)

1
  • 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. Commented Jul 9, 2013 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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