st4ck0v3rfl0w
st4ck0v3rfl0w

Reputation: 6755

Access only the first row of a 2d array

I'm running a RegEx to match all instances of the Twitter hashtag. It returns it just fine, but now I just want to loop through the first set and return my #hello, #world, #hello-world, not the second set.

Array
(
    [0] => Array
        (
            [0] => #hello
            [1] =>  #world
            [2] =>  #hello-world
        )

    [1] => Array
        (
            [0] => hello
            [1] => world
            [2] => hello-world
        )

)

Upvotes: 0

Views: 230

Answers (3)

Thomas Ahle
Thomas Ahle

Reputation: 31624

You don't even need to loop through. You can just do

return $matches[0];

This will return

Array (
        [0] => #hello
        [1] =>  #world
        [2] =>  #hello-world
    )

Upvotes: 1

soulmerge
soulmerge

Reputation: 75764

do you mean foreach($array[0] as $string) {...?

Upvotes: 2

Gumbo
Gumbo

Reputation: 655707

Specify $arr[0] as the array you want to iterate:

foreach ($arr[0] as $tag) {
    // …
}

Upvotes: 3

Related Questions