+ Reply to Thread
Results 1 to 3 of 3
This is a discussion on Help dealing with nested array results from SF API within the Salesforce Coding Discussions forums, part of the Salesforce category; First off, for various reasons, I'm stuck using NuSOAP with this particular
  1. #1
    cewyattjr is offline Junior Member
    Join Date
    Nov 2009
    Posts
    4

    Help dealing with nested array results from SF API

    First off, for various reasons, I'm stuck using NuSOAP with this particular client. But secondly what I've found is that when I query the Lead table, I get a different dimension array result when there is one record versus more than one. This makes it difficult to know how to run my nested foreach loops.

    For example, if I have a single record, the var_dump of my array looks like this:
    array(4) { ["type"]=> string(4) "Lead" ["id"]=> string(18) "00Q7000000WDBaXEAX" ["values"]=> array(4) { ["FirstName"]=> string(34) "Chuck another test record to leave" ["LastName"]=> string(31) "Wyatt leave for nurture testing" ["Email"]=> string(30) "nurturetest@wordimpressive.com" ["VFS_Trial_Begins__c"]=> string(10) "2010-09-05" } ["fieldsToNull"]=> NULL }

    On the other hand if I have more than one, I've got this, everything is wrapped around yet an additional array:
    array(2) { [0]=> array(4) { ["type"]=> string(4) "Lead" ["id"]=> string(18) "00Q7000000WDBaXEAX" ["values"]=> array(4) { ["FirstName"]=> string(34) "Chuck another test record to leave" ["LastName"]=> string(31) "Wyatt leave for nurture testing" ["Email"]=> string(30) "nurturetest@wordimpressive.com" ["VFS_Trial_Begins__c"]=> string(10) "2010-09-05" } ["fieldsToNull"]=> NULL } [1]=> array(4) { ["type"]=> string(4) "Lead" ["id"]=> string(18) "00Q7000000V6R2jEAF" ["values"]=> array(4) { ["FirstName"]=> string(13) "vreports test" ["LastName"]=> string(13) "vreports test" ["Email"]=> string(28) "vreports@howfishoilworks.com" ["VFS_Trial_Begins__c"]=> string(10) "2010-09-05" } ["fieldsToNull"]=> NULL } }
    What's the best way to handle? An example of the array looping code I've been working with is below. But of course it fails in the event there is only one record (!).

    Thanks!!

    -Chuck

    $response = obj2array($queryResult[records]);

    $arr = array($response);

    foreach ($arr as $key=>$temp) {


    foreach ($temp as $secondkey=>$temp2) {


    echo "got this: $secondkey = $temp2","<br><br>";

    // we transfer to variables we can use for mailing and logging:

    foreach ($temp2 as $thirdkey=>$temp3) {
    if ($thirdkey == 'id') { $sfid = $temp3; }

    if ($thirdkey == 'type') { $recordtype= $temp3; }



    echo "$sfid this one:" . "$thirdkey = $temp3","<br><br>";
    foreach ($temp3 as $fourthkey =>$temp4) {

    // we transfer to variables we can use for mailing and logging:
    if ($fourthkey == 'FirstName') { $firstname = $temp4; }
    if ($fourthkey == 'LastName') { $lastname = $temp4; }
    if ($fourthkey == 'Email') { $email = $temp4; }
    if ($fourthkey == 'VFS_Trial_Begins__c') { $trialbegins = $temp4;


    }

    }

    }

  2. #2
    cewyattjr is offline Junior Member
    Join Date
    Nov 2009
    Posts
    4
    I think I found the answer to this. Basically I check for record count on my query object and handle it like this, which seems to "start" the array pointer at the same location.

    if ($recordcount == '1') {
    $response = obj2array($queryResult);
    }
    if ($recordcount > '1') {

    $response = obj2array($queryResult[records]);

    }

  3. #3
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    Great Chuck and thanks for joining the site. Sorry I could not respond faster

+ Reply to Thread

Similar Threads

  1. Displaying Child Records in A Nested Query
    By alcom in forum Salesforce PHP Tutorials
    Replies: 12
    Last Post: 01-16-2010, 07:16 AM
  2. Getting nested field values
    By vdanvoye in forum Salesforce Coding Discussions
    Replies: 2
    Last Post: 06-01-2009, 08:16 AM
  3. Parsing confusing results from a relationship query
    By bakum in forum Salesforce Coding Discussions
    Replies: 2
    Last Post: 08-04-2008, 07:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO 3.5.2