循环通过两个数组?

时间:2013-09-30 14:11:38

标签: php

我有一个数组,我遍历它:

foreach($input['gallery'] as $galleryImg) {
}

但是在循环中,除了为$ input [' gallery']做一些事情之外,我还想为$ input [' galleryData']做点什么。

如何在同一循环中访问图库数据数组的每个部分?

foreach($input['gallery'] as $galleryImg) {

     //do something for $galleryImg
     //do something for $input['galleryData'][$i?]
}

输入转储:

{
    ["title"]=> string(0) ""
    ["date"]=> string(10) "30-09-2013"
    ["content"]=> string(0) ""
    ["galleryData"]=> string(149)
        "[
            {
                "comment":"$(this).data('comment')"
                ,"youTube":"$(this).data('youtube')"
            },
            {
                "comment":"$(this).data('comment')"
                ,"youTube":"$(this).data('youtube')"
            }
        ]"
    ["cover"]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7)
        {
            ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false)
            ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(14) "smallbunny.jpg"
            ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg"
            ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(2383)
            ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0)
            ["pathName":"SplFileInfo":private]=> string(14) "/tmp/phpBXVl5Q"
            ["fileName":"SplFileInfo":private]=> string(9) "phpBXVl5Q"
        }
    ["gallery"]=> array(2)
        {
            [0]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#10 (7)
                {
                    ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false)
                    ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "6empj.jpg"
                    ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg"
                    ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(116985)
                    ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0)
                    ["pathName":"SplFileInfo":private]=> string(14) "/tmp/phpsl4sNd"
                    ["fileName":"SplFileInfo":private]=> string(9) "phpsl4sNd"
                }
            [1]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#11 (7)
                {
                    ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false)
                    ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(14) "smallbunny.jpg"
                    ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg"
                    ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(2383)
                    ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0)
                    ["pathName":"SplFileInfo":private]=> string(14) "/tmp/phpA1u3vA"
                    ["fileName":"SplFileInfo":private]=> string(9) "phpA1u3vA"
                }
        }
}

1 个答案:

答案 0 :(得分:-1)

在主阵列上循环,并根据数组键添加特殊操作:

foreach($input as $key => $value) {
     if($key == 'gallery') {
        foreach($value as $galleryValue) {
            //Do something with $galleryValue;
        }
     }
     else if ($key == 'galleryData') {
        $galleryData = json_decode($value);
     }
}

对于更健壮的代码,您还可以在foreach之前检查$value是否为数组,以确保并避免错误