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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:52:00+00:00 2026-05-23T02:52:00+00:00

am working on php loops and i find myself that i need to write

  • 0

am working on php loops and i find myself that i need to write a nested loop bur when i try to combine foreach and for loop it yield to unexpected result
Here are my codes

       foreach ($district_ward as $key => $value) {
           $ward_ids = array_keys($district_ward[$key]);
           echo $key;
           for ($x = 0; $x < count($ward_ids)-1; $x++) {
               $district_village[$key]= array_merge($value[$ward_ids[$x]], $value[$ward_ids[$x+1]]);
           }               
        }

This gives me this

347

but when i print the value of $key within the for loop, that is

        foreach ($district_ward as $key => $value) {
           $ward_ids = array_keys($district_ward[$key]);
           for ($x = 0; $x < count($ward_ids)-1; $x++) {
   echo $key;
               $district_village[$key]= array_merge($value[$ward_ids[$x]], $value[$ward_ids[$x+1]]);
           }
        }

i get this

3
  • 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-23T02:52:01+00:00Added an answer on May 23, 2026 at 2:52 am

    I’m just going to guess that your 2nd and 3rd array are only one entry long. The condition for the loop specifies $x < count($ward_ids) - 1. If your array only has one entry, one – 1 will make it loop 0 times. In other words, your inner loop is not executed at all.

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

Sidebar

Related Questions

I am working with a PHP foreach loop and I need it to output
I am working with php and want to have a page that has a
I am new to JavaScript and didn't arrive to find a working script that
So here's something I find puzzling. I'm working on a Javascript that needs to
I am working on redesigning my site and find myself needing to display post
I am working on a pretty large PHP class that does a lot of
I have nested ajax calls that are not working in ie7. They work fine
Working with PHP Xpath trying to quickly pull certain links within a html page.
If you are working in PHP (or I guess any programming language) and using
at my working place (php only) we have a base class for database abstraction.

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.