Solved

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

Posted on 2013-06-30
4
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 110

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 110

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

732 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