0

I have an array which contains 3 different arrays.

$arrayAll = (

    0 => $array1,
    1 => $array2,
    2 => $array3
);

How can I loop through $arrayAll, displaying the first element of each sub-array(array1,array2,array3) on each itteration?

So, the output will be:

$array1[0],$array2[0],$array3[0],
$array1[1],$array2[1],$array3[1],
$array1[2],$array2[2],$array3[2]

and so on.. until all sublements are fetched.

EDIT:

$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
            }

        }

    }
}
3
  • Are the inner arrays all of the same length? Commented Oct 28, 2015 at 14:27
  • No, they can be different lengths Commented Oct 28, 2015 at 14:43
  • I have edited my answer below to support different lengths. It can be optimized, i'll think of something else if you need an optimized script Commented Oct 28, 2015 at 14:48

1 Answer 1

2
$maxLength = 0;

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

for($i=0; $i<$maxLength; $i++)
{
    foreach($arrayAll as $value)
    {
        if(isset($value[$i]))
        {
            echo $value[$i];    
        }

    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. Each of array1,array2,array3 requires a different HTML template, so inside of foreach($arrayAll as $value){} I added if ($value == array1){//use template 1}elseif(//use template 2) etc.. that works well and pull the correct template however I am having difficulties with itterating over $value[$i]. It contains a number of data items, which have to be extracted. I tried foreach($value[$i] as $dataItem){}. However, that produces errors. If you see what I mean?
can u update your answer with your array? is it an associative array? object? Clarify that and I'll help you with it
Essentially i cannot just echo the $value[$i] which is an array of its own, so I need to iterate over it somehow and foreach doesn't work on it, perhaps i have not structured it appropriately?
create an associative array like that: array('name'=>'valueofname', 'age'=>'valueOfAge'), and once you iterate over them, you can use $value[$i]['name'] for example
Edited the post based on your answer, so you can see the problem is with accessing the contents of $value[$i] and displaying it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.