Solved

Php nested array - How to get a value out

Posted on 2009-07-07
3
365 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

17 Experts available now in Live!

Get 1:1 Help Now