Solved

Php nested array - How to get a value out

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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.  …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 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