循环遍历数组并在之后分开

时间:2017-03-20 12:39:29

标签: php arrays

我从源代码中获得了一系列链接。我使用foreach循环遍历数组并将结果添加到新数组中。

问题是:我不希望所有结果都在一个数组中。但是在我循环之后,每个链接都有一个单独的数组。

我正在循环的数组:

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

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

)

foreach:

$sourceCats = array();

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

数组sourceCats现在看起来如何:

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

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

我希望它看起来像:

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

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

我尝试了一些但没有任何效果。这个想法清楚了吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

<?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);