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

  • Home
  • SEARCH
  • 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 7067255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:10:47+00:00 2026-05-28T05:10:47+00:00

$index = 0; foreach ($sxml->entry as $entry) { $array + variable index number here

  • 0
$index = 0;

foreach ($sxml->entry as $entry) {

    $array + variable index number here = array('title' => $title);

    $index++;
}

I’m trying to change an array name depending on my index count. Is it possible to change variable name (ie. $array1, $array2 $array3 etc.) in the loop?

Edit:

After the loop has finished, I will generate a number number (depending on the count of $index) and then use this array… probably it’s a stupid way of accomplishing what Im trying to do, but I don’t have a better idea.

  • 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-28T05:10:47+00:00Added an answer on May 28, 2026 at 5:10 am

    You might want to try this instead:

    $index = 0;
    
    $arrays = array();
    
    foreach ($sxml->entry as $entry) {
    
        $arrays[$index] = array('title' => $title);
    
        $index++;
    }
    

    While it is technically possible to do what you are asking, using an array of arrays will probably work better from you.

    This type of indexing is exactly what arrays are designed for, you have a lot of items and want to be able to refer to them by number.

    Unless you have a very specific reason to use the name of the variable to represent it’s number you will probably have a much simpler time using it’s index in the outer array.

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

Sidebar

Related Questions

Like iterrating a array, and using the index variable later instead of count() :
Is it possible to find the foreach index? in a for loop as follows:
My JSP contains the following snippet: <c:forEach items=${rulesForm.rules} var=rule varStatus=counter> <tr id=rules${counter.index} name=rules[${counter.index}]> rules
Possible Duplicate: Multiple index variables in PHP foreach loop Can we echo multiple arrays
Is it possible to get the row index from an arraylist using JSTL? <c:forEach
How can you refer to the index of an array in the foreach? My
Possible Duplicate: (C#) Get index of current foreach iteration Good morning, Is there any
I have this <c:forEach items=${countryList} var=var1 varStatus=counter> <input type=checkbox name=passports[${counter.index}].country_issue value=${var1.country_name} >${var1.country_name}</input> </c:forEach> Now
The code for index.phtml as follows: <?php foreach ($this->entries as $entry): ?> <?php echo
Currently I am doing this: $lines = file('data/index'); foreach ($lines as $value) list($title, $location)

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.