Remco A
Remco A

Reputation: 147

Loop through array and separate after

I got an array of links which I am getting from source code. I am looping through the array with a foreach loop and adding the results into a new array.

The problem is: I don't want all the results in one array. But for each link a separate array after I looped over it.

The array I am looping through:

Array
(
    [0] => Array
        (
            [0] => http://videos.volkswagen.nl/videos/videos/
        )

    [1] => Array
        (
            [0] => http://videos.volkswagen.nl/videos/service-videos/
        )

)

The foreach:

$sourceCats = array();

        foreach ($matchesAll as $links) {
            $strSourceAll = implode("|",$links);
            $source = file_get_contents("$strSourceAll");
            htmlspecialchars($source);
            $sourceCats[] = $source;
        }

How the array sourceCats looks now:

Array
(
    [0] => (source code from first link)

    [1] => (source code from second link)
)

How I want it to look like:

Array
    (
        [0] => Array
            (
                [0] => (source code from first link)
            )

        [1] => Array
            (
                [0] => (source code from second link)
            )
    )

I have tried a few things but nothing worked. Is the idea clear?

Any help will be much appreciated.

Upvotes: 1

Views: 59

Answers (1)

user1544541
user1544541

Reputation: 193

<?php
$finalsourceCats = array();
$counter_sourceCats = 0;

$matchesAll = array(
    0 => array(
        0 => "http://videos.volkswagen.nl/videos/videos/"
    ),
    1 => array(
        0 => "http://videos.volkswagen.nl/videos/service-videos/"
    )
);

foreach ($matchesAll as $links) {
    $sourceCats = 'sourceCats';
    $sourceCats = $sourceCats . "_" . $counter_sourceCats;
    $sourceCats = array();
    $strSourceAll = implode("|", $links);
    $source = file_get_contents("$strSourceAll");
    htmlspecialchars($source);
    $sourceCats[] = $source;
    $finalsourceCats[] = $sourceCats;
    $counter_sourceCats += 1;
}

echo "<pre>"; print_r($finalsourceCats);

Upvotes: 1

Related Questions