如何遍历这个PHP数组

时间:2022-09-23 07:43:12

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

我正在从GetResponse Email Marketing API检索信息,我的代码从数组中返回此数据:

array(2) {
  ["zAuW"]=>
  object(stdClass)#332 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "email@gmail.com"
    ["name"]=>
    string(23) "asw_getresponse_edition"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "email@gmail.com"
    ["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@gmail.com"
    ["name"]=>
    string(7) "test"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "email@gmail.com"
    ["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@gmail.com"
    ["name"]=>
    string(23) "asw_getresponse_edition"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "email@gmail.com"
    ["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:

我想知道我可以使用什么PHP代码循环遍历此数组并在下拉列表中显示信息。我试过这个,但这显然是错的:

$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.

任何帮助将不胜感激。

1 个解决方案

#1


1  

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'])

您的响应中有一组对象。所以,在你的foreach循环中你应该访问它的元素,比如$ value-> name(而不是$ 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

您的值没有id属性。因此,经过上述修正后,$ value-> id将为空。但是,$ key似乎是我的唯一标识符。如果是这种情况,在foreach循环中,您可以用$ key替换$ value-> id

$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>';

#1


1  

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'])

您的响应中有一组对象。所以,在你的foreach循环中你应该访问它的元素,比如$ value-> name(而不是$ 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

您的值没有id属性。因此,经过上述修正后,$ value-> id将为空。但是,$ key似乎是我的唯一标识符。如果是这种情况,在foreach循环中,您可以用$ key替换$ value-> id

$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>';