[英]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. 好的,这里的想法是拥有将SF_附加到数组中所有键名的代码。 I took my array (which is part of an object), flipped it, added the SF_, and flipped it back.
我拿走了数组(它是对象的一部分),将其翻转,添加了SF_,然后将其翻转回去。
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. 如您所说,
array_flip
将翻转值和键。 A PHP array cannot have multiple keys with the same name. 一个PHP数组不能有多个具有相同名称的键。 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). 进行翻转时,您将得到重复的键-这些值成为键,并且您的值不是唯一的(例如Email和AltEmail__c都具有相同的值)。
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.
因为您拥有共享相同价值的价值,所以您会在翻转时失去它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.