Solved

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

Posted on 2013-06-30
4
424 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

932 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

14 Experts available now in Live!

Get 1:1 Help Now