ne bu php kodu ile yanlış?

4 Cevap

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 sürecinde bazı alanları kaybettim ...

Burada ben ile başladı ne:

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)

Ve işte benim kod:

$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!

4 Cevap

Eğer çevirmek yaptığınızda, yinelenen tuşları ile sona - değerleri anahtarlar haline gelir ve değerler (örn. E-posta ve AltEmail__c hem de aynı değere sahip) benzersiz değildir.

Ziyade geri çevirme flip yaparak daha, sadece yeni bir dizi oluşturmak ve yeni anahtarlar ile değerleri kopyalayın:

$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;

Dediğin gibi array_flip, değerleri ve anahtarları çevirmek olacaktır. Bir PHP dizi aynı ada sahip birden tuşları olamaz. Saygısız önlemek için böyle bir şey deneyin:

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

Eğer nesne dizi olsun yol hakkında, bu bunu yapmak için uygun bir yoldur.

$SF = get_object_vars($response);

Bir diziye nesneyi dönüştürecek.

swapları anahtarları ve değerleri çevirin. Eğer aynı değeri paylaşan değerlere sahip, çünkü flip onları kaybetmek.