Jason
Jason

Reputation: 4957

How to iterate through this PHP array

I am retrieving information from the GetResponse Email Marketing API and my code returns this data from the array:

array(2) {
  ["zAuW"]=>
  object(stdClass)#332 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "[email protected]"
    ["name"]=>
    string(23) "asw_getresponse_edition"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "[email protected]"
    ["created_on"]=>
    string(19) "2014-03-29 07:27:46"
    ["from_name"]=>
    string(11) "1213456@"
  }
  ["z1Bi"]=>
  object(stdClass)#333 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "[email protected]"
    ["name"]=>
    string(7) "test"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "[email protected]"
    ["created_on"]=>
    string(19) "2014-03-29 02:54:51"
    ["from_name"]=>
    string(11) "123456@"
  }
}
object(stdClass)#330 (1) {
  ["zAuW"]=>
  object(stdClass)#334 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "[email protected]"
    ["name"]=>
    string(23) "asw_getresponse_edition"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "[email protected]"
    ["created_on"]=>
    string(19) "2014-03-29 07:27:46"
    ["from_name"]=>
    string(11) "123456@"
  }
}

Here is the code used to get this data:

require_once('GetResponseAPI.class.php');
$api = new GetResponse('YOUR_API_KEY');

// Account
$details = $api->getAccountInfo();
//var_dump($details);

// Campaigns
$campaigns = (array)$api->getCampaigns();
$campaignIDs = array_keys($campaigns);
$campaign = $api->getCampaignByID($campaignIDs[0]);
var_dump($campaigns, $campaign);

I would like to know what PHP code could I use to loop through this array and display the information in a dropdown list. I have tried this but it is oviously wrong:

$output .= '<select class="asw_select'.$field_class.'" name="'.$this->prefix.'_options['.$id.']" id="'.$this->prefix.'_options['.$id.']">';
foreach($campaign as $key => $value)
    $output .= '<option '.selected($current_mailing_list, $value['id'], false).' id="'.esc_attr($value['id']).'" value="'.$value['id'].'">'. __($value['name'], $this->prefix).'</option>';
$output .= '</select>';

Any help would be greatly appreciated.

Upvotes: 0

Views: 60

Answers (1)

mesutozer
mesutozer

Reputation: 2859

You have an array of objects in your response. So, in your foreach loop you should access it's elements like $value->name (not as $value['name'])

You do not have id attribute for your values. So after above correction, $value->id will be empty. However the $key seems like a unique identifier for me. If this is the case, within your foreach loop you can replace $value->id with $key

$output.= '<select class="asw_select'.$field_class.'" name="'.$this->prefix.'_options['.$id.']" id="'.$this->prefix.'_options['.$id.']">';
foreach($campaign as $key => $value)
    $output .= '<option '.selected($current_mailing_list, $key, false).' id="'.esc_attr($key).'" value="'.$key.'">'. __($value->name, $this->prefix).'</option>';
$output .= '</select>';

Upvotes: 1

Related Questions