php - Iterating over array of arrays and presenting the results

832

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
            }

        }

    }
}
305

Answer

Solution:

You're dealing with an associative array here, you can access it like that:

<?php

$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)
        {
            $post_id = $value[$i]['id'];
            $uploaded_by = $value[$i]['uploaded_by'];
            $text = $value[$i]['text'];
            $image = $value[$i]['image'];

            require 'template1.php';
        }
        elseif($value==$addsContent)
        {
            //template2
        }
        else
        {
           //template3
        }

    }

}
}
751

Answer

Solution:

You don't need theforeach.$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';
          }

People are also looking for solutions to the problem: php - Manipulate JSON

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.