Iterating through Multi Dimensional Arrays

Events happening in the community are now at Drupal community events on www.drupal.org.
thepocketgeek's picture

I am trying to get a list of certain values from within a multidimensional array:

[field_vehicle_models_ref] => Array(
            [0] => Array(
                    [nid] => 175
                    [view] => Turtletop Limocoach ** This is the Value I am trying to get returned by the foreach loop
                )

            [1] => Array(
                    [nid] => 176
                    [view] => Turtletop Odyssey ** This is the Value I am trying to get returned by the foreach loop
                )
        )

What I would like to generate is a nice little unordered list featuring the [view] field so that it looks basically like this:

<ul>
     <li>Turtletop Limocoach</li>
     <li>Turtletop Odyssey</li>
</ul>

I tried to use the foreach construct to get the data but I am not getting any results (this is because I did it wrong..)

Here is the code I ginned up:

print "<ul>";

//check the first level of the array
foreach(array($node->field_vehicle_models_ref) as $array){
  
   // dig into sub array for treasure...
  foreach($array as $key => $value){
      print $value."/br>";
  }//end foreach

} //end foreach
print "</ul>";

Which returned the following:

  Array/br>Array/br>Array/br>Array/br>Array/br>Array/br>

So how do I grab a value from within a sub array?

Comments

Huzzah!

thepocketgeek's picture

I think I was able to solve my issue:

Here is the code I used to get the return I was looking for:

print "<ul>";

//check the first level of the array
$array = $node->field_vehicle_models_ref;

foreach($array as $key => $value){
 
   // set each sub array up for search
    $array2 = $value;
 
   // get the treasure from the sub array
foreach($array2 as $key => $value){
     if($value == $view){
           return $view;
      }//endif
   }//end foreach

   // print out the returned value
    print "<li>";
  print_r($value);
   print "</li>";         
} //end foreach
print "</ul>";

Here is what it returned:

* Turtletop Limocoach
* Turtletop Odyssey
* TurtleTop Odyssey XL & XLT
* Turtletop Spirit
* Turtletop Terra Traveler
* Turtletop Van Terra \ Terra Transport

Exaclty what I needed it to. Which is just peachy. Now was there a simpler / faster way to do this? I am always looking for a simpler way to problem solve.

foreach($node->field_vehicle_

garethsprice's picture

foreach($node->field_vehicle_models_ref as $vehicle_model_ref) {
  printf('<li>%s</li>', $vehicle_model_ref['view']);
}

Orlando, FL Web Developer | http://www.garethsprice.com/

Wow. That was so much cleaner

thepocketgeek's picture

Wow. That was so much cleaner and it gave me an excuse to look up the printf string function...
Thanks!

Thanks a lot!

stefan81's picture

Two questions:

1)
How can I use implode with this?
I would like to separate each item with a comma:
car1,car2,car3

2)
How can I skip the first one [0]?

Implode usage

caelon's picture

The implode statement is simply:

$implode_array = implode(",", $array);

Implode doesn't have a way to skip the first item but there are multiple ways to do it. I think there would be debate about which is the most efficient but if your array isn't large, it likely doesn't matter. This is how I would do it.

$new_array = array_slice($array, 1); //will create a new array without the first element
$implode_array = implode(",", $new_array);

Don VanDemark
Agile Process Manager, Blue Shield of California

Thank you!

stefan81's picture

It just worked fine:)

$active_trail = menu_get_active_trail();
foreach(array_slice($active_trail,1) as $items) { $out[] = $items['mlid']; }
$crumb_mlid = implode(',', $out);
$vars['custom_crumb_mlid'] = $crumb_mlid;

Florida

Group organizers

Group categories

Florida Topics

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: