tags:

views:

87

answers:

4

Ok, the idea here is to have code that will append SF_ to all key names in an array. I took my array (which is part of an object), flipped it, added the SF_, and flipped it back.

Somewhere in the process I lost some fields...

here's what I started with:

object(stdClass)[12]
  public 'Affiliate_Code__c' => string 'XX-TXUJC3' (length=9)
  public 'AltEmail__c' => string '[email protected]' (length=22)
  public 'City' => string 'Mobile' (length=6)
  public 'Email' => string '[email protected]' (length=22)
  public 'Fax__c' => string '251-300-1234' (length=12)
  public 'FirstName' => string 'Benny' (length=5)
  public 'LastName' => string 'Butler' (length=6)
  public 'Phone' => string '251-300-3530' (length=12)
  public 'PostalCode' => string '36606' (length=5)
  public 'State' => string 'AL' (length=2)
  public 'Street' => string '851 E I-65 Service Rd' (length=21)
  public 'test1__c' => float 1
array
  'SF_Affiliate_Code__c' => string 'XX-TXUJC3' (length=9)
  'SF_Email' => string '[email protected]' (length=22)
  'SF_City' => string 'Mobile' (length=6)
  'SF_Fax__c' => string '251-300-1234' (length=12)
  'SF_FirstName' => string 'Benny' (length=5)
  'SF_LastName' => string 'Butler' (length=6)
  'SF_Phone' => string '251-300-3530' (length=12)
  'SF_PostalCode' => int 36606
  'SF_State' => string 'AL' (length=2)
  'SF_Street' => string '851 E I-65 Service Rd' (length=21)

And here's my code:

$response = $mySforceConnection->query(($query));



   foreach ($response->records as $SF) {
   }


    var_dump($SF);
    $SF = array_flip($SF);
   foreach ($SF as $key => $value){

    $SF[$key] = 'SF_'.$value;
    }

   $SF = array_flip($SF);
   echo "<pre>";
  var_dump($SF);
  echo "</pre>";
  extract($SF);

Any idea? I'm new to OO programming of any sort, and I'm sure this has something to do with it. I'm so stupid I have to do:

foreach ($response->records as $SF) { }

because I don't know how to get to that array any other way. Help! Thanks!

+1  A: 

array_flip will flip the values and keys, as you said. A PHP array cannot have multiple keys with the same name. Try something like this to avoid flipping:

<?php
$SF = array();
foreach($response->records as $key => $value)
{
    $SF['SF_' . $key] = $value;
}

About the way you get to the array in the object, this is the proper way to do it.

JoostK
you mean foreach and not for.
easement
@easement True, have not been using PHP in a while, did not really think about it. Thanks for your comment, just edited my answer.
JoostK
A: 

flip swaps keys and values. because you have values that share the same value, you lose them in the flip.

easement
+1  A: 
$SF = get_object_vars($response);

Will transform your object into an array.

Julian Aubourg
+1  A: 

When you do the flip, you end up with duplicate keys - the values become the keys, and your values are not unique (e.g. Email and AltEmail__c both have the same value).

Rather than doing a flip then flipping back, just create a new array and copy the values in with the new keys:

$SF_new = array();
foreach($SF as $key => $value ) {
   $SF_new['SF_' . $key] = $value;
}

// And if you want to continue using the $SF name...
$SF = $SF_new;
Eric Petroelje
Well, you all answered correctly, it was hard to choose. I'm chosing Eric because it's the ost like how I code.Thanks!