1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting specific data from an array

Discussion in 'PHP' started by Silver89, Nov 5, 2010.

  1. #1
    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?
     
    Silver89, Nov 5, 2010 IP
  2. jimmy4feb

    jimmy4feb Peon

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    jimmy4feb, Nov 5, 2010 IP
  3. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    bencummins, Nov 6, 2010 IP