2

I have the following regex:

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

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

Column 7 is an array made up of many different strings. I want to match all the urls and store them in a single array $matches such that I have all the matches from each element in a single array like the example below.

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

    )

When I print_r($matches[$i]) it looks like I get a multidimensional array which just overwrites itself. How can I just get a one dimensional array with all the urls one after the other?

I hope that makes sense!

Actual Output I'm getting:

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 Answer 1

1

I think you need to add up all elements you get in for loop to a new array - Something like -

$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

See array_merge

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.