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 9246519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:30:31+00:00 2026-06-18T09:30:31+00:00

I have an array in my PHP which I am iterating over twice, doing

  • 0

I have an array in my PHP which I am iterating over twice, doing different things each time.
The array is called $data.
I have 2 foreach loops like this:

foreach($data as $item){
//doing stuff
}
foreach($data as $item){
//doing different stuff
}

I cant combine the 2 as this is how the application is but I want to move data into the $data array in the first iteration which will appear in the second iterations. The only way I could think of was below. e.g.

foreach($data as $item){
$data[]['new key']='my new data';
}
foreach($data as $item){
var_dump($item['new key']);//print 'my new data'
}

Is there a better way to do this?

  • 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-06-18T09:30:33+00:00Added an answer on June 18, 2026 at 9:30 am

    Sounds like you want to pass array BY REFERENCE.
    This means you are treating the array NOT like a typical array (a copy) but like a pointer to the $data array.
    It is similar to how you might use an object (which is not a copy but an instance itself).

    You can implement this with:

    foreach($data as &$item){
        $item['new var'] ='my new data';
    }
    
    foreach($data as $item){
        var_dump($item['new key']);//print 'my new data'
    }
    

    hope that helps

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

Sidebar

Related Questions

I have an Array in PHP which contains data from database. And it prints
I have a PHP array which has several different fields. For some of the
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have a array stored in a PHP file in which I am storing
i have one php file which process adding of record in Database fro array.
I have an array in PHP which looks like that: $test = array('3' =>
So i have a page called region.php, which in the header, loads JQuery, the
I have an array in PHP which looks like this: Array ( [2] =>
I have a long php array which I loop through and create a form
I have a loop in my script, which iterates over an array - myArray

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.