2

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
                                        )

                                )

                        )


                )

        )

)

4 Answers 4

3

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";
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes i just realized myself that i stopped fixing the code right after the first mistake. i corrected the inner-foreach too in my edit.
Post has been edited to give a more elaborative explanation on $node =& $output['node'];
1

$value is the array, not $key['main_topic']

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

Comments

0

Try this, you need the additional key:

echo $value['main_topic'][$mainkey]['topic_title'];

1 Comment

$main_topic is the iterated object. No need to specify a key.
0

You're getting your array sections confused.

Try (and I haven't tested this) :

echo $output['node_title']."\n";
foreach ($output['fields'] as $key=>$value)
{
    switch ($key)
    {
        case 'title':
            echo $value."\n";
            break;
        case 'main_topic':
            if (count($value) > 0)
            {
                foreach ($value as $main_block)
                {
                    echo "\t".$main_block['topic_title']."\n";
                    if (array_key_exists('subfield',$main_block)!==FALSE)
                    {
                        foreach ($main_block['subfield'] as $subfield_block)
                        {
                            echo "\t\t".$subfield_block['asset_title']."\n";
                        }
                    }
                }
            }
            break;
        default:
            break;
    }
}

Comments

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.