Solved

Php nested array - How to get a value out

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
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…

810 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