php数组多维

时间:2012-11-29 14:26:43

标签: php arrays

我刚刚编写了一个抓取脚本,它可以工作。现在我得到这个输出:

Array
(
    [0] => Array
        (
            [0] => example stuff
            [1] => example stuff
            [2] => example stuff
            [3] => example stuff
        )

[1] => Array
    (
        [0] => /spellen/thumbs/a/aladdinwildride_6524.jpg
        [1] => /spellen/thumbs/t/toystory3_3476.jpg
        [2] => /spellen/thumbs/f/fred-flintstone-bobsled_2180.jpg
        [3] => /spellen/thumbs/m/madagascar-2.jpg
    )

[2] => Array
    (
        [0] => spel Alladin Wide Rit
        [1] => spel Toy Story 3
        [2] => spel Bedrock Bobsledding blowout
        [3] => spel Madagascar 2 Ontsnap naar Afrika

    )

[3] => Array
    (
        [0] => /spellen/cartoons/2096/alladin-wide-rit.html
        [1] => /spellen/cartoons/1989/toy-story-3.html
        [2] => /spellen/cartoons/1362/bedrock-bobsledding-blowout.html
        [3] => /spellen/cartoons/237/madagascar-2-ontsnap-naar-afrika.html

    )

[4] => Array
    (
        [0] => l
        [1] => l
        [2] => l
        [3] => l

    )

[5] => Array
    (
        [0] => Speel Alladin Wide Rit online
        [1] => Speel Toy Story 3 online
        [2] => Speel Bedrock Bobsledding blowout online
        [3] => Speel Madagascar 2 Ontsnap naar Afrika online

    )

[6] => Array
    (
        [0] => e
        [1] => e
        [2] => e
        [3] => e

    )

[7] => Array
    (
        [0] => /images/thumb_frame_top.gif
        [1] => /images/thumb_frame_top.gif
        [2] => /images/thumb_frame_top.gif
        [3] => /images/thumb_frame_top.gif

    )

[8] => Array
    (
        [0] => f
        [1] => f
        [2] => f
        [3] => f

    )

现在我想要一个简单的foreach,每个$matches[1][0]$matches[2][0]$matches[3][0]都会返回给我。

我一直试图让这个好几个小时,但没有运气。有人能帮助我吗?

5 个答案:

答案 0 :(得分:0)

$result = array();
foreach ($matches as $match) {
    $result[] = array(
        $match[1],
        $match[2],
        $match[3],
    );
    // or unset($match[0]); if you don't need the original array anymore
    // or array_splice($match, 0, 1); if you also want to rearrange the keys
}

答案 1 :(得分:0)

foreach($matches as $match){

    // $match[0] is what you need

}

如果要将结果包装在新数组中,可以将原始数组缩减为:

$newArray = array_reduce($matches, function(&$result, $match){

  $result[] = $match[0];

});

答案 2 :(得分:0)

这是你想要的吗?

for ($i = 0; $i < count($matches); $i++)
{
    print $matches[$i][0];
}

答案 3 :(得分:0)

foreach ($matches as $j => $match) {
   foreach ($match as $i => $mi) {
       if ($i != 0) unset($matches[$j][$i]);
   }
}

答案 4 :(得分:0)

如果你只想要前三个:

$newArray = array(
    $matches[1][0],
    $matches[2][0],
    $matches[3][0]
);

结果:

    Array
    (
        [0] => /spellen/thumbs/a/aladdinwildride_6524.jpg
        [1] => spel Alladin Wide Rit
        [2] => /spellen/cartoons/2096/alladin-wide-rit.html
    )