dongmai6666 2014-03-30 01:00
浏览 19
已采纳

如何遍历这个PHP数组

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@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:

$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条回答 默认 最新

  • douyuan1752 2014-03-30 02:11
    关注

    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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错