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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:24:14+00:00 2026-05-24T03:24:14+00:00

I follow struggling with bidimensional arrays and foreach loops. The following code shows a

  • 0

I follow struggling with bidimensional arrays and foreach loops. The following code shows a magazines array, each of them points to an unidimensional Articles array. Right now is storing three Articles for each magazine (just the Article’s Title). I would like to know how to associate a Bidimensional array so that can I store the Author and PagesNumber besides the Title, for each article belonging a magazine. Could you point me in the right direction? Thanks in advance.

<?php

// Create a magazines array
$magazines = array();

// Put 5 magazines on it
for($x=0;$x<5;$x++)
    $magazines[] = "Magazine " . $x ;

// Associate articles array to each magazine
foreach($magazines as $magazine){
    $articles[$magazine] = array();
    for($x=0;$x<3;$x++)
        $articles[$magazine][] = $magazine . " - Article Title " . $x ;
}

// List them all
foreach($magazines as $magazine){
    echo $magazine . " has these articles: <br>";
    foreach($articles[$magazine] as $article)
        echo $article . "</br>";
}
?>
  • 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-24T03:24:14+00:00Added an answer on May 24, 2026 at 3:24 am

    Maybe you should structure it a little different:

    $magazines = array();
    
    //create magazines
    for($x=0;$x<5;$x++) {
      $magazines[] = array('name' => 'Magazine ' . $x, 
                           'year' => '2011', 
                           'articles' => array());
    }
    
    //fill in some articles
    foreach ($magazines as &$magazine) {
      for($x=0;$x<3;$x++) {
        $magazine['articles'][] = array('article_titel' => $magazine['name'] . " - Article Title " . $x, 
                                        'article_author' => 'author ' . $x,
                                        'article_page' => $x);
      }
    }
    
    //output
    foreach($magazines as $magazine) {
      echo 'The Magazine ' . $magazine['name'] . ' from ' . $magazine['year'] . ' contains the articles : ';
      foreach($magazine['articles'] as $article) {
        echo $article['article_titel'] . ' by ' . $article['article_author'] . ' on page ' . $article['article_page'];
      }
    }
    

    A little explaination:
    Magazines is a normal array.
    Then you add associative arrays to it (magazines).
    Each magzines-array has an articles-field, which is a normal array, which you add associative arrays to again (articles).

    So now, you iterate throug magazines, and each magazine has an array of articles which you can iterate.

    That means: Magazines is an array of single magazines, one magazine is an associative array, which also contains an array of articles. An article is an associative array again.

    I hope that sounds not too confusing… 😉

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

Sidebar

Related Questions

Follow up to this question . I have the following code: string[] names =
I was looking at this article and am struggling to follow the VB.NET example
I'm struggling a little to work out how to follow the relation and count
This is a follow-on question to this: Getting XML attributes in PHP I'm struggling
I have tried follow this through, but I am struggling to get it into
I'm looking at Process:w3wp*:% Processor Time in PerfMon and am struggling to follow something.
How to configure Netbeans PHP to follow Zend Coding Standard? I'm struggling with this
I am struggling with the follow thing. I have an user page where people
I'm still struggling to get my sample code working on a 64 bit machine.
I struggling with how to create my own custom dialog. I follow this example

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.