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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:39:42+00:00 2026-05-29T23:39:42+00:00

function example() { foreach ($choices as $key => $choice) { # \__ both should

  • 0
function example()
{

    foreach ($choices as $key => $choice) {       # \__ both should run parallel
    foreach ($vtitles as $keystwo => $vtitle) {   # /

        $options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected  
.'>'. check_plain($choice) .'</option>';


    } // end of vtitle    
    } // end of choice

    return $options;
}

Answers to some of the below questions and what I am trying to achieve.

  1. Array $choices is not numerically indexed.
  2. Array $vtitle is numerically indexed.
  3. They won’t be shorter than each other as I have code which will take care of this before this code runs.
  4. I am trying to return $options variable. The issue is that $choices[0] and $vtitle[0] should be used only once. Hope I was able to express my problem.
  5. I do not want to go through the $vtitles array once for each value in $choices.

@hakre: thanks I have nearly solved it with your help.

I am getting an error for variable $vtitle:

InvalidArgumentException: Passed variable is not an array or object, using empty array    
instead in ArrayIterator->__construct() (line 35 of /home/vishal/Dropbox/sites/chatter/sites
/all/themes/kt_vusers/template.php).

I am sure its an array this is the output using print_r

Array ( [0] => vishalkh [1] => newandold ) 

What might be going wrong ?

The below worked for me , thank you hakre

while
(
(list($key1, $value1) = each($array1))
&& (list($key2, $value2) = each($array2))
)
{
printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
}
  • 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-29T23:39:44+00:00Added an answer on May 29, 2026 at 11:39 pm

    It does not work the way you outline with your pseudo code. However, the SPL offers a way to iterate multiple iterators at once. It’s called MultipleIterator and you can attach as many iterators as you like:

    $multi = new MultipleIterator();
    $multi->attachIterator(new ArrayIterator($array1));
    $multi->attachIterator(new ArrayIterator($array2));
    
    foreach($multi as $value)
    {
        list($key1, $key2) = $multi->key();
        list($value1, $value2) = $value;
    }
    

    See it in action: Demo

    Edit: The first example shows a suggestion from the SPL. It has the benefit that it can deal with any kind of iterators, not only arrays. If you want to express something similar with arrays, you can achieve something similar with the classic while(list()=each()) loop, which allows more expressions than foreach.

    while
    (
        (list($key1, $value1) = each($array1))
        && (list($key2, $value2) = each($array2))
    )
    {
        printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
    }
    

    Demo (the minimum number of elements are iterated)


    See as well a related question: Multiple index variables in PHP foreach loop

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

Sidebar

Related Questions

foreach($arrayOne as $value){ do function } In the above example, I'd like to pass
Consider the following code example: TempList.ForEach(Function(obj) obj.Deleted = True End Function) And this one:
If I run a standard cURL_multi_exec function (example below), I get all cURL handles
It is valid JavaScript to write something like this: function example(x) { Here is
For example: (function() { var proxied = window.eval; window.eval = function() { return proxied.apply(this,
I have an example function below that reads in a date as a string
So I have some openGL code (such code for example) /* FUNCTION: YCamera ::
I'm pretty new to XQuery and I'm trying to write an example function that
I have a simple question related to one-line programming. First an example: function test(a)
Example: /** * This function will determine whether or not one string starts with

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.