Solved

Php nested array - How to get a value out

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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