Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7417367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:41:19+00:00 2026-05-29T07:41:19+00:00

I need to print the below array structure as: Node Title 1 topic 1

  • 0

I need to print the below array structure as:

Node Title 1

  topic 1

  topic 2

  topic 3

  topic 4

    asset title1 

    asset title2

    asset title3

How can i do using foreach – PHP

What i have done is :

foreach($output['fields'] as $key => $value) {
        if($key == 'title') {
            echo $value;
        }
        if(count($value['main_topic'])) {
            foreach($value['main_topic'] AS $mainkey => $main_topic) {
                echo $main_topic['topic_title'];
            }
        }
    }

The above syntax is printing the title. But not the main_topic array.

Array
(
    [fields] => Array
        (
            [nid] => 136
            [node_title] => Node title 1
            [node_type] => curriculum
            [title] => Node title 1
            [main_topic] => Array
                (
                    [0] => Array
                        (
                            [row_id] => 136
                            [topic_id] => 411847
                            [weight] => 10
                            [topic_title] => topic 1
                        )

                    [1] => Array
                        (
                            [row_id] => 136
                            [topic_id] => 411839
                            [weight] => 2
                            [topic_title] => topic 2
                        )

                    [2] => Array
                        (
                            [row_id] => 136
                            [topic_id] => 411840
                            [weight] => 3
                            [topic_title] => topic 3
                        )

                    [3] => Array
                        (
                            [row_id] => 136
                            [topic_id] => 411841
                            [weight] => 4
                            [topic_title] => topic 4
                            [subfield] => Array
                                (
                                    [1] => Array
                                        (
                                            [asset_title] => asset title 1
                                        )

                                    [2] => Array
                                        (
                                            [asset_title] => asset title 2
                                        )

                                    [3] => Array
                                        (
                                            [asset_title] => asset title 3
                                        )

                                )

                        )


                )

        )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:41:22+00:00Added an answer on May 29, 2026 at 7:41 am

    That is because you are iterating over all $output['fields'].
    There will never be a $value with key 'main_topic' because the key 'main_topic' is contained in the $output['fields'] array and thus exists only as $key in your foreach. The array you want is $value

    Your code should be like:

    foreach($output['fields'] as $key => $value) {
            if($key == 'title') {
                echo $value;
                continue;
            }
            if($key == 'main_topic' && is_array($value)) {
                foreach($value as $main_topic) {
                    echo $main_topic['topic_title'];
                }
            }
        }
    

    To complete this answer with a full solution (including asset titles), below is how I would write it.

    Because $output['fields'] is the starting point and to make the code more readable, I create a reference to the starting node using the =& operator so the array is not copied in memory. I do the same with the inner foreachs. Since we are not modifying data, referencing the variables is sufficient and consumes less memory and CPU:

    if (is_array($output['fields'])) {
        $node =& $output['fields'];
        echo $node['title'];
        if(is_array($node['main_topic'])) {
            foreach($node['main_topic'] as &$main) {
                echo $main['topic_title'];
                if(is_array($main['subfield'])) {
                    foreach($main['subfield'] as &$asset) {
                        echo $asset['asset_title'];
                    }
                }
            }
        } 
    }
    else {
        echo "no menu";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to print a receipt from my web based apps using dot matrix
Need to store values from foreach loop into an array, need help doing that.
Given array a= [1,4,5,9,2].I need to find/print combinations of two values where sum =
I have an array whose structure is like $data = array{0=>'abc',1=>xyz,2=>tqs} Now I need
Need help with copying array object to a temp array object using a for
i need help on my code below. Im using codeigniter as my framework for
I have this array below and need to know the var to get the
I need to print out data into a pre-printed A6 form (1/4 the size
I need to print the date in the format of mm/dd/yyyy. if the date
I need to print records in a grid view, and need to preview it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.