1

How i can convert multidimensional array into a single array in php? I spent a few hours to find a solution but i couldn't find a solution that works for me. Here is my code:

<?php

$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
        "http" => array(
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
        )
    )
);

$source_url = 'http://netsparker.com/';
$html = file_get_contents($source_url,false, stream_context_create($arrContextOptions));
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');


foreach ($links as $link) {
    $result_url = $link->getAttribute('href');


    if (!preg_match('/^https?:\/\//', $result_url)) {
        $result_url = $source_url . preg_replace('/^\//', '', $result_url);


    }
    $array2 = array($result_url);
    print_r($array2);
}



?>

Array
(
    [0] => http://github.com/#start-of-content
)
Array
(
    [0] => https://help.github.com/articles/supported-browsers
)
Array
(
    [0] => https://github.com/
)
Array
(
    [0] => http://github.com/join?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home
)
Array
(
    [0] => http://github.com/features
)
Array
(
    [0] => http://github.com/features/code-review/
)
Array
(
    [0] => http://github.com/features/project-management/
)

....

I want to turn it into:

Array
(
    [0] => http://github.com/
    [1] => http://github.com/collections
    [2] => http://github.com/topics
    [3] => http://github.com/explore
    [4] => http://github.com/enterprise
    .......
) 

I tried with foreach,ArrayIterator,flatten but it seems to don't work. I also tried to transform result_url into an array but also doesn't worked.

2 Answers 2

1

Try pushing the values onto a single array in your foreach loop:

$array2 = [];
foreach ($links as $link) {
    $result_url = $link->getAttribute('href');

    if (!preg_match('/^https?:\/\//', $result_url)) {
        $result_url = $source_url . preg_replace('/^\//', '', $result_url);
    }

    $array2[] = $result_url;
}
print_r($array2);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried and it gives even more arrays with the same values in it. imgur.com/a/xHwANlJ
Make sure your print_r is outside of your foreach loop. It looks like you are calling print_r after every loop iteration.
Note how the print_r in my answer comes after the closing brace for the loop.
0

Directly push your sanitized urls into the result array.

$array2 = [];
foreach ($links as $link) {
    $array2[] = preg_replace(
        '/~^/~',
        $source_url,
        $link->getAttribute('href')
    );
}
var_export($array2);

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.