Jump to content

Accessing Array Values Individually


proggR

Recommended Posts

Is there any way to access each individual array element individually with something as simple as a loop?

I'm actually using a two dimensional array created with array_chunk($input, 5);

From there I want to have one loop for the first index and a second embedded loop for the second index

So I want:

 

for ($i = 0;$i<=$max;$i++){

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

      echo $array[$i][$j];

  }

}

 

but when I do that it just prints array[1], array[2] etc.

Any help? I've read some about for ($var as $varname) or something like that but forget. can i just use a simple loop like i have?

Thanks in advance. I haven't worked on this for a while so I thought I'd ask.

 

Link to comment
Share on other sites

foreach ($array as $key1)

-------------------------

   This loop basically does this:

 

$arrayLength=count($array);

for ($x=0; $x<$arrayLength; $x++) {

echo $array[$x];

}

 

Having foreach ($array as $value) { foreach ($value as $subvalue) { } } simply does the same thing except for multidimensional arrays. That is only one leve down. So here is the code and what it would do:

$array[fun][test]=5;
$array[fun][test2]=6;
$array[morefun][test]=1;
foreach ($array as $key) 
{
foreach ($key as $value) 
 { 
 echo $key.">".$value."<br />";
 }
}

Output:

fun>5

fun>6

morefun>1

 

Link to comment
Share on other sites

Ya I was actually just reading that. I'm not sure if this will work for me for what I need.

The first index represents a page number and the second index represents the post on that page.

I'll need to access them independantly from eachother.

What I want to happen is when I'm on page 3 it will go to the third element in the first index (the index that holds the page number) and I will be able to loop through each post index within that.

So:

 

//page 3

 

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

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

}

 

is essentially what I want to do. $post holds the filename of each post and I'll be including them 5 at a time onto a page. I'm not sure how I could use the foreach for that. I was kind of hoping to be able to access them more like this if possible.

Link to comment
Share on other sites

Oh, duh. Thanks!

Is there any reason why arrays in php don't seem to play nice with a normal for loop? In C/Java I know I'd be able to access them this way. Apples and oranges obviously, just wondering if theres a reason or if maybe I was just having bad luck.

Thank you a lot though. I'll make the changes tonight and see how it works out.

Link to comment
Share on other sites

The following

foreach($categories as $key => $category)
{
    echo 'Category #' . ($key+1) . '<br />';

    foreach($category as $page)
    {
        echo $page . '<br />';
    }

    echo '<hr />';
}

does the same as

for ($i = 0;$i<=$max;$i++){
   for ($j = 0;$j<=5;$j++){
      echo $array[$i][$j];
   }
}

 

$categoriesis formatted like so:

// category 1
$categories[0][0] = 'page1';
$categories[0][1] = 'page2';
$categories[0][2] = 'page3';

// category2
$categories[1][0] = 'page1';
$categories[1][1] = 'page2';
$categories[1][2] = 'page3';

Link to comment
Share on other sites

PHP works fine with arrays. Your logic you posted is wrong...

 

Doing a loop like

 

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

 

that loop will execute one time... Because you are saying while variable i is less than or equal to 1, do this. After the first loop, i = 2..

Link to comment
Share on other sites

PHP works fine with arrays. Your logic you posted is wrong...

 

Doing a loop like

 

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

 

that loop will execute one time... Because you are saying while variable i is less than or equal to 1, do this. After the first loop, i = 2..

 

Sorry I posted that wrong. It should have been ($i=1;$i<=$max;$i++). $max would just hold the number of pages that I need to use. But I've never had it work. Not even if I've replaced it with a numerical value instead.

Link to comment
Share on other sites

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.