0

I use PHP and have an array that looks like this:

Array
(
    [34] => Array
    (
        [slug] => my_slug
        [title] => my_title
    )
    [33] => Array
    (
        [slug] => my_slug2
        [title] => my_title2
    )
)

I also need to loop out the sets, key 34 with values and key 33 with values. I could loop them out with a foreach but in my case I can't use a foreach loop.

Problem - Loop by index?

Because I can't use a foreach loop in my case I need to come up with something where I can loop them by index.

My own thoughts

if I could get the array keys by index it would be fine. Is it possible, how?

<?php print_r( $my_array[index][0] ); ?>
<?php print_r( $my_array[index][1] ); ?>

Reasons

The reason why I can't use a foreach is that I am depending on a counter. If the counter says 3 it wants to get the fourth key (arrays starts with 0).

Example 1

$counter = 1;

Then it should return the contents of [33] because it's on the index 1.

Example 2

$counter = 0;

Then it should return the contents of [34] because it's on the index 0.

8
  • 2
    I don't get what your problem is exactly.. Commented Feb 14, 2013 at 15:02
  • $my_array[$counter]['slug'] ? Commented Feb 14, 2013 at 15:02
  • Yes, you can get the array keys with array_keys php function. Commented Feb 14, 2013 at 15:03
  • Why not use an iterating for loop? for ($i = 0, $count = count$(my_array); $i < $count; $i++ { // do something with $my_array[$i] } Commented Feb 14, 2013 at 15:04
  • @EM-Creations I added 2 examples. Commented Feb 14, 2013 at 15:06

3 Answers 3

1

Solution

Get the keys out of the array with array_keys and then use to counter to find the index you want to iterate over. You can then use a simple foreach loop to iterate over the sub array.

$keys     = array_keys($array);
$index    = $keys[$counter];
$subArray = $array[$index];
foreach($subArray as $key => $value) {
    // do stuff
}

Explanation

For the array you gave in the example array_keys($array) returns

array(
    0 => 34,
    1 => 33
);

So we can use counter on this new array to figure out the index of in the actual array we want to iterate over.

$index = $keys[$counter];

So if $counter = 0 then $index is now 34. Now it's simple enough to use $index on the original array to get the sub array we want to iterate over.

$subArray = $array[$index];

In this example with $counter being 0 the variable $subArray now holds

array(
    'slug'  => 'my_slug'
    'title' => 'my_title'
);

Not using foreach

Its fairly simple to adapt this answer to work without a foreach loop. Read the array_keys documentation and think about how that function could allow other types of loops to iterate over arrays with string keys.

References

array_keys documentation

Sign up to request clarification or add additional context in comments.

Comments

1

The reason why I can't use a foreach is that I am depending on a counter. If the counter says 3 it wants to get the fourth key (arrays starts with 0).

No problem:

var_dump($array[array_keys($array)[3]]);

(This requires PHP 5.4 because of the array dereferencing syntax)

For PHP 5.3 or earlier:

$keys = array_keys($array);
var_dump($array[$keys[3]]);

Comments

1

Probably this is what you are looking for...

$counter = 3; // for example
$keys = array_keys($my_array);
$needed_entry = $keys[ $counter ];

print_r( $my_array[ $needed_entry ] );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.