Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:28:01+00:00 2026-05-27T11:28:01+00:00

I have a set of categories I want to loop through, and set a

  • 0

I have a set of categories I want to loop through, and set a dynamic variable/array as the page loops.

The code I currently have is:

foreach($categories as $category)
{
    $category_ads[] = Advert::listforPageArea(2, 1, $category['id']);
}

The Advert::listforPageArea function returns an object of an advert based on what category is chosen.

I want to be able to calculate the expiry date of each advert to use elsewhere.

I’m struggling to explain exactly what I want, but I simply want to set custom content within a loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:28:01+00:00Added an answer on May 27, 2026 at 11:28 am

    What you’re searching for is called ‘variable variables’.

    See http://foundationphp.com/blog/2010/12/18/understanding-php-variable-variables/

    The linked blog entry also answers the question of variable scope (contrary to Geoffroy’s answer, variable variables are valid outside of the loop they are created in) and states (correctly) that this method is highly insecure without validating the variable names.

    Could you go with an array within an array instead? $arr['cat1']['cat2']

    EDIT: how to do this with multidimensional arrays (see http://php.net/manual/en/language.types.array.php)

    $ads = array(); // not explicitly needed, but clarifying
    // ... some code
    foreach($categories as $category)
    {
        if(!array_key_exists($category, $ads)) { $ads[$category] = array(); } // not explicitly needed
        $ads[$category['name?']][] = Advert::listforPageArea(2, 1, $category['id']);
        // or (see comment below)
        $ads[$category][] = Advert::listforPageArea(2, 1, getCategoryField($category, 'id'));
    }
    

    !!! Important: You’re using $category like a variable one time and like an associative array the other time. You can’t simply evaluate an array to a string. I guess your array $category may contain some field name?, as it seems to contain a field named id. You’d have to use that field name in $ads[$category['field???']][] or your line Advert::listforPageArea(2, 1, $category['id']); is not correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of categories that a user can choose from. Each category
I have a set of custom PropertyDescriptor that I want to add categories too
So...I have a large data set with a variable that has many categories. I
Let's say I have this DBSet public DbSet<Category> Categories { get; set; } and
I have an array of categories where id is the id of the category,
I have a simple SQL table which defines a set of hierarchical categories and
I have a select input: f.select :category_id, nested_set_options(Category, @categories) {|i| #{'-' * i.level} #{i.name}
I have set a background on the data-role=page element like so <div data-role=page style=background:
I have a table that holds nested categories. I want to avoid duplicate names
I currently have a data model set up where each School object has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.