Solved

How to loop through this kind of an array returned from mailchimp.

Posted on 2013-06-30
4
423 Views
Last Modified: 2013-07-02
Hi,
Using the mailchimp API I'm returning the array below. It contains the email addresses and their open counts for a particular email campaign. I'm having a hard time trying to loop through it so I can insert each email and corresponding open count into my db. Can someone show me an example of looping through this thing to do an insert?

array(2) { [0]=> array(2) { ["email"]=> string(24) "john@mysite.com" ["open_count"]=> int(1) } [1]=> array(2) { ["email"]=> string(23) "bill@mydomain.com" ["open_count"]=> int(3) } } 

Open in new window


Thanks as always,
tj
0
Comment
Question by:tjyoung
  • 2
  • 2
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39288752
This appears to be an array of arrays.  Iterate over the top array to get each "sub-array" by number.  Iterate over each "sub-array" to get the elements named "email" and "open_count".

array(2) 
{ [0]=> array(2) 
  {  ["email"]=> string(24) "john@mysite.com" 
     ["open_count"]=> int(1)
  } 
  [1]=> array(2) 
  {   ["email"]=> string(23) "bill@mydomain.com" 
      ["open_count"]=> int(3) 
  }
} 

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 39293505
Hi,
I've tried hundred variations only getting stuff like: arrayarray and a myriad of other mistakes. I'd post my attempts but none of them seemed close or simply failed.

cleaning up the arrays I appreciate, should've done that posting my question.

Can you give me an example of iterating through this? I can figure out adding to a DB I should think but I can't for the life of me get to the correct data.

Its a bit of a grey area in my 'home schooling' efforts.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39293606
Some good learning resources here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

In PHP when you echo an array, PHP rather unhelpfully prints "array."  See if this code sample helps get you closer

Please see http://www.laprbass.com/RAY_temp_tjyoung.php

<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28171952.html

/* DATA STRUCTURE
array(2)
{ [0]=> array(2)
  {  ["email"]=> string(24) "john@mysite.com"
     ["open_count"]=> int(1)
  }
  [1]=> array(2)
  {   ["email"]=> string(23) "bill@mydomain.com"
      ["open_count"]=> int(3)
  }
}
*/

// CREATE THE TEST DATA FROM THE DESCRIPTION
$arr = array
( 0 => array
       ( 'email'      => 'john@mysite.com'
       , 'open_count' => 1
       )
, 1 => array
       ( 'email'      => 'bill@mydomain.com'
       , 'open_count' => 3
       )
)
;

// DOES IT LOOK RIGHT? (YES)
var_dump($arr);

// ITERATE OVER THE ARRAY AND SUB-ARRAYS
foreach ($arr as $key => $sub)
{
    echo PHP_EOL . "MAIN ARRAY KEY: $key";
    foreach ($sub as $key => $val)
    {
        echo PHP_EOL . "   SUB ARRAY KEY: $key VALUE: $val";
    }
    echo PHP_EOL;
}

Open in new window

Best, ~Ray
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39293732
Thank you very much. Makes perfect sense once you see it...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now