Currently I have a large array that brings back values in the following format: Array ( [0] => CSContact Object ( [first_name] => [last_name] => [emails] => Array ( [0] => Array ( [value] => email@email.com [type] => WindowsLiveID ) ) [phones] => Array ( ) ) [1] => CSContact Object ( [first_name] => [last_name] => [emails] => Array ( [0] => Array ( [value] => email@email.com [type] => WindowsLiveID ) [1] => Array ( [value] => email1@email1.com [type] => Business ) ) [phones] => Array ( ) ) Code (markup): I'm trying to just return the emails, one per line so: email@email.com email@email.com email1@email1.com Code (markup): But I'm struggling with foreach, any ideas?
I think, there is some problem in your array so you need to modify array so it looks something like following example then you will able to use foreach. Just run this code & you will understand, how its working... <?php $array = array( array( "firstName" => "First Name 1", "LastName" => "Last Name 1", "email" => array( "value" => "email1@email.com", "type" => "yahoo" ) ), array( "firstName" => "First Name 2", "LastName" => "Last Name 2", "email" => array( "value" => "email2@email.com", "type" => "MSN" ) ), ); echo "<pre>"; print_r($array); echo "</pre>"; foreach($array as $key => $value) { foreach($value as $key1 => $value1) { if($key1 == "email") { foreach($value1 as $key2 => $value2) { if($key2 == "value") { echo $value2."<br>"; } } } } } ?> Code (markup): Thanks, Akash
Assuming your array is a variable called email_array, something like the following should work.. <? for ($i = 0; $i < count($email_array); $i++) { foreach ($email_array[$i]["emails"] AS $k=>$v) { echo $v["value"]."\n"; } } ?> PHP: