php数组帮助 - 如何将此数组放在另一个数组中?

时间:2011-02-13 12:22:47

标签: php arrays

我有3个主键的数组。 图像,下载和视频。

这是一个转储示例:

array(3) {
["images"]=>
  array(1) {
    [0]=>
    array(3) {
      ["cod_teamFk"]=>
      string(2) "51"
      ["attachement"]=>
      string(22) "210111011642_51_57.jpg"
      ["bonus"]=>
      string(1) "0"
    }
}
["downloads"]=>
  array(1) {
    [0]=>
    array(3) {
      ["cod_teamFk"]=>
      string(2) "51"
      ["attachement"]=>
      string(22) "210111011732_51_23.zip"
      ["bonus"]=>
      string(1) "0"
    }
}
["videos"]=>
  array(1) {
    [0]=>
    array(1) {
      ["youtube"]=>
      array(1) {
        [0]=>
        string(150) "object video"
      }
    }
}

为了产生这个,我在这里使用了这些方法:

function SplitProof($proof) {

                $arr = array();
            $arr["images"] = array();
            $arr["downloads"] = array();
                $arr["videos"] = array();
            $arrDownloads = array("zip"=>1, "rar"=>1, "pdf"=>1,"doc"=>1,"docx"=>1,"ppt"=>1,"pptx"=>1);
            $arrImagem = array("jpg"=>1,"jpeg"=>1, "gif"=>1,"png"=>1);



            if (count($proof))
                {
                    foreach ($proof as $r)
                    {

                        $file = "./lib/teams/".$r["attachement"];

                        if (is_file($file))
                        {
                            $pathinfo = pathinfo($file);
                            $ext = strtolower($pathinfo["extension"]);

                            if ($arrImagem[$ext] == 1)
                            {
                                $fileThumb = "./lib/teams/thumb".$r["attachement"];

                                if (!file_exists($fileThumb))
                                {
                                    $thumb = new Thumbnail("lib/teams/".$r["attachement"]);
                                    $thumb->cropFromCenterNoSquare(230, 153);
                                    $thumb->show(100,$fileThumb);
                                }

                                $arr["images"][] = $r;
                             }

                             if ($arrDownloads[$ext] == 1)
                             {
                                $arr["downloads"][] = $r;
                             } 

                        }

    // ISSUE HERE - DON'T KNOW HOW TO PROPERLY ADD THOSE VIDEOS HERE
                        if ($this->_splitVideo($r['proof']) != false)
                        {
                            $arr["videos"][] = $this->_splitVideo($r['proof']);
                        }
                    } //eo foreach

                }//eo if

                return $arr;
            }


                /**
                 * @desc Finds a video reference a convert it into a video object, finally,
                 * store in into an array.
                 * 
                 * @param <type> $proof
                 * @return string
                 */
                private function _splitVideo($proof) {


                    $video_links = array();

                    if (preg_match_all("'(http://)?(www\.)?(youtube|vimeo|videos\.frog)\.([a-z0-9_/?&+=.]+)'is",$proof,$n))
                    {

                        foreach ($n[3] as $key => $site)
                        {

                            $urlCorreto[$key] = str_replace(array("http://www.","http://", "www."),"",$n[0][$key]);

                            $urlCorreto[$key] = "http://" . $urlCorreto[$key];

                            $data = parse_url($urlCorreto[$key]);

                            switch(strtolower($data['host']))
                            {
                                case "youtube.com":

                                    parse_str($data["query"],$result);

                                    $videoId = $result["v"];

                                    $objetoYoutube = '<iframe title="YouTube video player" width="380" height="225" src="http://www.youtube.com/embed/'.$videoId.'" frameborder="0" allowfullscreen></iframe>';

                                    $video_links[$site][] = $objetoYoutube;

                                break;


                                case "vimeo.com":

                                    $vimeoPath = $data["path"]; 

                                    $objetoVimeo = '<iframe src="http://player.vimeo.com/video'.$vimeoPath.'" width="380" height="225" frameborder="0"></iframe>';

                                    $video_links[$site][] = $objetoVimeo;

                                break;


                                case "videos.frog.es":

                                    $frogPath = $data["path"];

                                    $objetofrog = '<embed src="http://rd3.videos.frog.es/play?file=http://rd3.videos.frog.es'.$frogPath.'/mov/1" type="application/x-shockwave-flash" allowFullScreen="true" width="380" height="225"></embed>';

                                    $video_links[$site][] = $objetofrog;

                                break;
                            }




                        }

                        return $video_links;

                    } else {

                        return false;
                    }

                }

如你所见,&#34;图像&#34;和&#34;下载&#34;与证据记录$ r相关联。 但是,视频不是。

我想请求你的帮助,以便允许视频密钥与$ r集成,就像图像和下载一样,这样,我可以拥有,而不是我现在的视频转储类似的东西:

["videos"]=>
  array(1) {
    [0]=>
    array(3) {
      ["cod_teamFk"]=>
      string(2) "51"
      ["bonus"]=>
      string(1) "0"
      ["attachement"]=>
      NULL
      ["videoObject"]=> 
      string(17) "video object here"
    }
}

1 个答案:

答案 0 :(得分:1)

变化:

if ($this->_splitVideo($r['proof']) != false)
{
  $arr["videos"][] = $this->_splitVideo($r['proof']);
}

为:

if ($this->_splitVideo($r['proof']) != false)
{
  $r["videoObject"] = $this->_splitVideo($r['proof']);
  $arr["videos"][] = $r;
}

现在只是调整splitVideo方法只输出视频对象而不是数组的问题。

:)

雨披!!!