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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:29:02+00:00 2026-06-13T08:29:02+00:00

Possible Duplicate: Iterate through two associative arrays at the same time in PHP I

  • 0

Possible Duplicate:
Iterate through two associative arrays at the same time in PHP

I have two arrays $name[] and $type[]. I want to print those arraya through foreach loop like this,

<?php
foreach($name as $v && $type as $t)
{
echo $v;
echo $t;
}
?>

I know this is wrong way then tell me correct 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-13T08:29:03+00:00Added an answer on June 13, 2026 at 8:29 am

    You cannot do this like that. You need to do one of the following:

    1. use a for loop and “share” the index variable, or
    2. manually iterate with each and friends, or
    3. zip the two arrays together with array_map

    Example with for:

    for ($i = 0; $i < count($name); ++$i) {
        echo $name[$i];
        echo $type[$i];
    }
    

    Possible issues: The arrays need to be numerically indexed, and if they are not the same length it matters a lot which array you use count on. You should either target the shorter array or take appropriate measures to not index into the longer array out of bounds.

    Example with each:

    reset($name);  // most of the time this is not going to be needed,
    reset($type);  // but let's be technically accurate
    
    while ((list($k1, $n) = each($name)) && (list($k2, $t) = each($type))) {
        echo $n;
        echo $t;
    }
    

    Possible issues: If the arrays are not the same length then this will stop processing elements after the “shorter” array runs out. You can change that by swapping || for &&, but then you have to account for one of $n and $t not always having a meaningful value.

    Example with array_map:

    // array_map with null as the callback: read the docs, it's documented
    $zipped = array_map(null, $name, $type);
    
    foreach($zipped as $tuple) {
        // here you could do list($n, $t) = $tuple; to get pretty variable names
        echo $tuple[0]; // name
        echo $tuple[1]; // type
    }
    

    Possible issues: If the two arrays are not the same length then the shorter one will be extended with nulls; you have no control over this. Also, while convenient this approach does use additional time and memory.

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

Sidebar

Related Questions

Possible Duplicate: How to iterate through two lists in parallel? I have 2 lists:
Possible Duplicate: deep copy NSMutableArray in Objective-C ? I have two arrays of length
Possible Duplicate: iterate through pairs of items in python list I have a list
Possible Duplicate: How to iterate by row through a mysql query in php After:
Possible Duplicate: Best way to iterate through a directory in java? I want to
Possible Duplicate: How to iterate by row through a mysql query in php I
Possible Duplicate: how can I iterate through two lists in parallel in Python? i
Possible Duplicate: How can I iterate through nested arrays in Perl? I am trying
Possible Duplicate: Parse XML sequentially in PHP I want to be able to iterate
Possible Duplicate: How do I iterate through each element in an n-dimensional matrix in

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.