phparraysforeach

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
        )

)

Solution

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

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