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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:34:58+00:00 2026-05-18T20:34:58+00:00

I was wondering if its possible, to loop through an array but have each

  • 0

I was wondering if its possible, to loop through an array but have each key as a variable?

My current code is below, with an example expected output:

<?php

   $arr = array(array('id' => 24, 'name' => 'luigi'), array('id' => 12, 'name' => 'luiginsd'));

   foreach ($arr as $value) {

      echo $id . '<br />';

   }

/*

which would output:

24<br />
12<br />

*/

?>

All help is appreciated.

  • 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-18T20:34:59+00:00Added an answer on May 18, 2026 at 8:34 pm

    Use extract:

    foreach ($arr as $value) {
       extract($value);
       echo $id.'<br />';
    }
    

    extract will iterate through an associative array and initialize a variable (presumably using variable variables) of the same name as the key in the array in the current scope containing the associated value.

    Just for fun, here’s what I think extract does under the hood:

    foreach($array as $key => $value) {
       $$key = $value;
    }     
    

    Note that extract does not necessarily import these variables into global scope, they are imported into the current symbol table.

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

Sidebar

Related Questions

I was wondering if its possible to have Jira update a ticket's status to
I'm wondering if its possible to add new class data members at run-time in
I was wondering if its possible to inject a thread into a remote app
I'm wondering if it's possible to define a foreign key in a models.py file
Yeah, its a bit on this side of pointless, but I was wondering... I've
I was wondering if I could have some pseudo code for working out the
Wondering if its possible to Moq the Prism EventAggregator Let's take the EventAggregator Quickstart
I was wondering if its possible to make a single comboBox not visible or
I'm building a website using WordPress and am wondering if its possible to display
What im trying to do is a little different, im wondering, if its possible

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.