Posted by thepocketgeek on August 25, 2010 at 3:32pm
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!
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:
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_
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
Wow. That was so much cleaner and it gave me an excuse to look up the printf string function...
Thanks!
Thanks a lot!
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
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!
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;