简体   繁体   中英

what's wrong with this php code?

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 'benny@oxpublishing.com' (length=22)
  public 'City' => string 'Mobile' (length=6)
  public 'Email' => string 'benny@oxpublishing.com' (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 'benny@oxpublishing.com' (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!

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.

$SF = get_object_vars($response);

将您的对象转换成数组。

When you do the flip, you end up with duplicate keys - the values become the keys, and your values are not unique (eg 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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM