Jump to content

Recommended Posts

Two Dimensional Arrays in PHP seem to be confusing me and I'm not sure where I'm going wrong.

Basically what I want to do is have a two dimensional array hold the filename of a blog post (I'm scripting a basic blog engine in case you were wondering). The posts are all in a folder as an individual .php file that will be include()d into the blog page. Each page will have 5 posts on it. I've scripted counting the number of files in the folder and read each of the filenames into a normal array. I've got it so it knows how many pages there will be based on the 5 posts per page. The only thing I can't figure out is the 2d array.

The first index of the array will be the page number and the second index will be the post number on that page so essentially what I'm trying to do is:

 

$counter = 1;

for($i=1;$1<=$numpages;$i++){

     for($j=1;$j<=5;$j++){

          $post[j]=$fileURL[$counter];

          $counter++;

     }

}

 

I don't fully understand arrays in PHP so I just wrote it the way I'm used to declaring them. If someone could modify this to be a working example it would be great. I'm sure I can work through the logic if I see a working example of what I'm doing.

As of right now it seems to be my only stumbling block. Also, is it necessary to use foreach() to read the values or could a normal for() loop handle it?

Thanks in advance for any help.

 

 

*edit* there is no database or anything like that either. I just have the files saved into a folder and will be reading them in from there. I may change to a DB later but for now its just a flat file system (except not the normal flat PHP file systems that I've seen in other blog engines because I didn't like the way they were setup)

Link to comment
https://forums.phpfreaks.com/topic/107303-two-dee-array-with-nested-loop/
Share on other sites

How have you set up your array's?

 

A foreach is easiest but not essential (but what you've shown is more than 2d!):

for($i=1;$1<=$numpages;$i++){
    echo "No. ".$post[0];
    echo "MSG: ".$post[1];
}

 

I'm lost, are you just including a single page of posts or all? In that page is it just php code arrays?

Well since the first index is for the page number i can then use that number from a link (like if the person clicks "page 3") and feed the value 3 to another script that will pull up the five posts within $post[3]. That would basically just be a loop that would look like:

 

for ($i=1;$i<=5;$i++){

include("$post[3][$i];);

}

 

It would obviously need some formatting so that they're aligned vertically but that's beside the point.

The files that I'm reading in are all formatted html files saved as .php so when I include them it already has all the content and its just being pasted into the page. This allows me to use the same data if someone wants to view that post individually or for when I have it setup by keyword (course subject). That's all been worked out, if I can get the array to work.

I'm pretty sure its still two dimensional since it only has two indexes. Its like a chart where the rows represent page number and the columns are the post on the page and the value in the chart is the url that I'm trying to assign to it. Its like a DB without a DB I guess.

I haven't really setup my arrays. I'm not sure how to declare them. I'm used to arrays in Java, and to a lesser extent, C. Basically all I would need to do is:

String array[][] = new String [100][10]; *the numbers are the largest index it can handle*

Although I may be wrong for declaring String array but thats generally the way.

Anyway. I just need to know how to declare a 2d numerical array and how I can assign values to it using loops, similar to the way i did in the OP.

i'm not entirely clear on the goal, but maybe something like this?

 

$post = array();
$counter = 1;
for($i=1;$1<=$numpages;$i++){
     $post[$i] = array();
     for($j=1;$j<=5;$j++) {
          $post[$i][$j]=$fileURL[$counter];
          $counter++;
     }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.