如何将数组的正则表达式结果存储到非多维数组中

时间:2016-12-11 11:49:52

标签: php arrays multidimensional-array

我有以下正则表达式:

$regex = '/https?\:\/\/[^(\"|\\\) ]+/i';

for ($i = 0; $i < count($column7); ++$i)
    {
    preg_match_all($regex, $column7[$i], $matches[$i]);
    print_r($matches[$i]);
    }

第7列是由许多不同字符串组成的数组。我想匹配所有的url并将它们存储在一个单独的数组$ matches中,这样我就可以得到单个数组中每个元素的所有匹配项,如下例所示。

Array
    (
        [0] => https://domain.comf3aeaf
        [1] => https://ureasdjlkfjasldkf.com
        [2] => http://heelooo.com
        [3] => https://www.asdfasdfasd.com
        [4] => https://asdfafrgasrgas.com
        [5] => http://rgtfgsdagf.com
        [6] => http://asfgdfhgasdgafsd.com
        [7] => http://asdghdthgaterge.com
        [8] => https://asdgsdhdsthaerararrrr.com
        [9] => https://t.com
        [10] => http://abc.cmo

    )

当我print_r($matches[$i])时,我看起来像是一个只能覆盖自身的多维数组。我怎样才能一个接一个地获得所有网址的一维数组呢?

我希望这是有道理的!

我得到的实际输出:

Array
(
    [0] => Array
        (
            [0] =>     https://domain.comf3aeaf
            [1] => https://ureasdjlkfjasldkf.com
            [2] => http://heelooo.com
        )

)
Array
(
    [0] => Array
        (
            [0] => https://www.asdfasdfasd.com
            [1] => https://asdfafrgasrgas.com
            [2] => http://rgtfgsdagf.com
            [3] => http://asfgdfhgasdgafsd.com
            [4] => http://asdghdthgaterge.com
            )

    )
Array
(
    [0] => Array
        (
            [0] => https://asdgsdhdsthaerararrrr.com
            [1] => https://t.com
            [2] => http://abc.cmo
        )

)

1 个答案:

答案 0 :(得分:1)

我认为你需要将你所获得的所有元素添加到一个新数组中 - 比如 -

$newArr = array();
for ($i = 0; $i < count($column7); ++$i) {
  preg_match_all($regex, $column7[$i], $matches[$i]);
  $newArr = array_merge($newArr, $matches[$i][0]); // or try $newArr += $matches[$i][0];
}

print_r($newArr); //should be your required array

请参阅array_merge