仅访问二维数组的第一行

问题描述 投票:0回答:3

我正在运行正则表达式来匹配 Twitter 主题标签的所有实例。它返回得很好,但现在我只想循环第一组并返回我的

#hello, #world, #hello-world
,而不是第二组。

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

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

)
php arrays foreach
3个回答
3
投票

指定

$arr[0]
作为要迭代的数组:

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

2
投票

你的意思是

foreach($array[0] as $string) {...
吗?


1
投票

您甚至不需要循环。 你就可以做

return $matches[0];

这会回来

Array (
        [0] => #hello
        [1] =>  #world
        [2] =>  #hello-world
    )
© www.soinside.com 2019 - 2024. All rights reserved.