Solved

Php nested array - How to get a value out

Posted on 2009-07-07
3
363 Views
Last Modified: 2012-05-07
Hi,

I have a nested array in php:
[field_linkout] => Array
        (
            [0] => Array
                (
                    [nid] => 57
                    [view] => <a href="/content/about">About</a>
                )

            [1] => Array
                (
                    [nid] => 15
                    [view] => <a href="/content/learning" class="active">Learning</a>
                )

            [2] => Array
                (
                    [nid] => 55
                    [view] => <a href="/content/organising">Organising</a>

                )

        )


I need to display only the links in the [view] element.

This is how far I've got:

<?php
    if(is_array($node->field_linkout)){
      foreach ($node->field_linkout as $linkoutarray ) {
            foreach ($linkoutarray as $linkdata => $link)
            {
                print $link[view] . '<br/>';
            }
      }
    } //
  ?>

  </div>

But it is displaying the first value of both the [nid] and [view] elements. I just need the [view] value.

Any help would be really appriciated.
Thanks, Lk
0
Comment
Question by:leekey
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Michal-Drozd earned 500 total points
ID: 24793035
your code is wrong, try this:
    if(is_array($node->field_linkout)){

      foreach ($node->field_linkout as $linkoutarray ) {

         print $linkoutarray[view] . '<br/>';

      }

Open in new window

0
 
LVL 3

Expert Comment

by:Michal-Drozd
ID: 24793055
little explanation:
This is array of array (2 levels). And your code was passing it like it was array of array of array (3 levels).
0
 

Author Comment

by:leekey
ID: 24793069
Thanks Michal - I can see what you mean with regards to 3 levels.
have now changed my code and its working correctly.
Thanks again, Lk
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now