遍历数组和presenting结果数组 [英] Iterating over array of arrays and presenting the results

查看:110
本文介绍了遍历数组和presenting结果数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何访问$值的内容[$ i]这是一个数组。没有运气下面的表格使用的foreach。

我们的想法是通过$ contentArray循环并显示一个项目从每次迭代每个子阵列

  $ addsContent = $ Adds-> selectAdds(10);
$ sharedArticlesContent = $ SharedContent-> getSharedContent($ topic_selected,$ filter_selected);
$ blogPostsContent = $ BlogPosts-> getRecentBlogPostsByTopic(业务);$ contentArray =阵列(    $ sharedArticlesContent,
    $ addsContent,
    $ blogPostsContent
);
的foreach($ contentArray为$值)
    {
        如果(计数($值)> $最大长度)
        {
            $最大长度=计数($值);
        }
    }为($ I = 0; $ I< $最大长度; $ I ++)
{
    的foreach($ contentArray为$值)
    {
        如果(使用isset($价值[$ i]))
        {
            如果($价值== $ sharedArticlesContent){
                $数据= $价值[$ i];
                的foreach($ sharedArticlesContent为$数据){                    $ POST_ID = $数据['身份证'];
                    $ uploaded_by = $数据['uploaded_by'];
                    $文字= $数据['文本'];
                    $图像= $数据['形象'];                    需要'template1.php';              }
            } ELSEIF($价值== $ addsContent){
                //则Template2
            }其他{
               // template3
            }        }    }
}


解决方案

您不需要的foreach $数据是一个关联数组,你不通过它需要循环。

 如果($价值== $ sharedArticlesContent){
            $数据= $价值[$ i];
            $ POST_ID = $数据['身份证'];
            $ uploaded_by = $数据['uploaded_by'];
            $文字= $数据['文本'];
            $图像= $数据['形象'];            需要'template1.php';
          }

How can I access the contents of $value[$i] which is an array. No luck using foreach in the form below.

The idea is to loop through $contentArray and display one item from each sub-array on every iteration.

$addsContent = $Adds->selectAdds(10);
$sharedArticlesContent = $SharedContent->getSharedContent($topic_selected, $filter_selected);
$blogPostsContent = $BlogPosts->getRecentBlogPostsByTopic("business");

$contentArray = array(

    $sharedArticlesContent,
    $addsContent ,
    $blogPostsContent
);


foreach($contentArray as $value)
    {
        if(count($value)>$maxLength)
        {
            $maxLength = count($value);
        }
    }

for($i=0; $i<$maxLength; $i++)
{
    foreach($contentArray as $value)
    {
        if(isset($value[$i]))
        {
            if($value==$sharedArticlesContent){
                $data = $value[$i];
                foreach($sharedArticlesContent as $data){

                    $post_id = $data['id'];
                    $uploaded_by = $data['uploaded_by'];
                    $text = $data['text'];
                    $image = $data['image'];

                    require 'template1.php';

              }
            }elseif($value==$addsContent){
                //template2
            }else{
               //template3
            }

        }

    }
}

解决方案

You don't need the foreach. $data is an associative array, you don't need to loop through it.

        if($value==$sharedArticlesContent){
            $data = $value[$i];
            $post_id = $data['id'];
            $uploaded_by = $data['uploaded_by'];
            $text = $data['text'];
            $image = $data['image'];

            require 'template1.php';
          }

这篇关于遍历数组和presenting结果数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆