值数组仅提供最后一个

时间:2014-03-25 17:30:45

标签: php arrays foreach

 array(4) { [109]=> array(11) { ["ID"]=> string(3) "109" ["name"]=> string(29) "unsplash_52cd96f512830_1.jpeg" ["path"]=> string(113) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/unsplash_52cd96f512830_1.jpeg" ["url"]=> string(97) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52cd96f512830_1-150x150.jpeg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(89) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52cd96f512830_1.jpeg" ["title"]=> string(24) "unsplash_52cd96f512830_1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } [113]=> array(11) { ["ID"]=> string(3) "113" ["name"]=> string(29) "unsplash_52d65d1ebeeb2_1.jpeg" ["path"]=> string(113) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/unsplash_52d65d1ebeeb2_1.jpeg" ["url"]=> string(97) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52d65d1ebeeb2_1-150x150.jpeg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(89) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_52d65d1ebeeb2_1.jpeg" ["title"]=> string(24) "unsplash_52d65d1ebeeb2_1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } [132]=> array(11) { ["ID"]=> string(3) "132" ["name"]=> string(29) "unsplash_523ae1f5502d6_1.jpeg" ["path"]=> string(113) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/unsplash_523ae1f5502d6_1.jpeg" ["url"]=> string(97) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_523ae1f5502d6_1-150x150.jpeg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(89) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/unsplash_523ae1f5502d6_1.jpeg" ["title"]=> string(24) "unsplash_523ae1f5502d6_1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } [163]=> array(11) { ["ID"]=> string(3) "163" ["name"]=> string(12) "building.jpg" ["path"]=> string(96) "/Users/framecero/Framecero_Clientes/MIMO/MAMP/fashionist/wp-content/uploads/2014/03/building.jpg" ["url"]=> string(80) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/building-150x150.jpg" ["width"]=> int(150) ["height"]=> int(150) ["full_url"]=> string(72) "http://localhost:8888/fashionist/wp-content/uploads/2014/03/building.jpg" ["title"]=> string(8) "building" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["alt"]=> string(0) "" } }

我需要在foreach中获取url并且我正在使用此代码:

<?php global $post;$medias = rwmb_meta( 'mimo_imgadv', 'type=image_advanced' );?>
<?php  } else {$medias = '';}; ?>
<?php if ($medias !== ''){ ?>
<div  class="cn_wrapper flexslider" >
    <ul id="allimg_id<?php the_Id(); ?>" class="format_images slides">
        <?php foreach($medias as $media){

        // check if string ends with image extension
            if (preg_match('/(\.jpg|\.png|\.bmp)$/', $media['url'])) {
                echo '<li class="selector">';
                ?>
                <a class="mimo-link-overimage" href="<?php the_permalink(); ?> " >
                         <div class="mimo-image-hover"><i class="fa fa-file-o"></i></div> 

                <?php
                echo '<img src="' . $media['url']  . '" alt=""/>';
                echo '</a>';
                echo '</li>';
            // check if there is youtube.com in string
            }           
        }       

        ?>
    </ul>
</div>

但它只给了我最后一个网址。

任何人都知道代码发生了什么?

1 个答案:

答案 0 :(得分:0)

您只需编辑正则表达式即可检测数组中的文件类型。您正在寻找类型.jpg,.png和.bmp,但这些类型的数组中只有一个文件。这就是为什么你只得到一个结果的原因。其他三个图像是.jpeg。以下是包含其他文件的已编辑代码:

<?php foreach($medias as $media){
    // check if string ends with image extension
    if (preg_match('/(\.jpg|\.jpeg|\.png|\.bmp)$/', $media['url'])) {
        echo '<li class="selector">';
        //remaining code unchanged...
?>