這可能是不可能的,但是知道這兩種方法都是很好的。引用PHP對象鍵作爲函數參數
我正在尋找使用一種方法來運行一個在我寫的自定義類中重複的過程。
一個函數的參數的,目標,需要參照的對象鍵在$值,所以在下面的代碼段,其涉及$值 - >目標。
public function format_array($post_id, $object, $values, Target, $meta_value) {
$array = (array) $object;
$feed_values = array();
if (!empty($array)) {
foreach ($values as $value) {
$feed_values[] = $value->Target;
}
}
unset($array);
$db_values = get_post_meta($post_id, $meta_value, true);
$result = array_diff($feed_values, $db_values);
if (!empty($result)) {
$this->update_post_meta($post_id, $meta_value, $feed_values);
}
}
我知道這個代碼是目前破...
可以這樣做?
編輯
參考值 - $>目標,其中$值低於對象的實例,我試圖訪問 '評論',所以$值 - >註釋:
object(stdClass)#411 (33) {
["Facilities"]=>
object(stdClass)#413 (1) {
["FacilityInfo"]=>
array(6) {
[0]=>
object(stdClass)#414 (4) {
["ID"]=>
string(21) "xxxxx"
["PropertyID"]=>
string(21) "xxxxx"
["Name"]=>
string(5) "xxxxx"
["Comment"]=>
string(9) "xxxxx"
}
[1]=>
object(stdClass)#415 (4) {
["ID"]=>
string(21) "xxxxx"
["PropertyID"]=>
string(21) "xxxxx"
["Name"]=>
string(15) "xxxxx"
["Comment"]=>
string(20) "xxxxx"
}
}
}
["Photos"]=>
object(stdClass)#420 (1) {
["PhotoInfo"]=>
array(5) {
[0]=>
object(stdClass)#421 (6) {
["ID"]=>
string(21) "xxxxx"
["PropertyID"]=>
string(21) "xxxxx"
["MainPhoto"]=>
bool(true)
["Name"]=>
string(8) "xxxxx"
["Type"]=>
string(5) "Photo"
["PhotoUrl"]=>
string(94) "xxxxxx"
}
[1]=>
object(stdClass)#422 (6) {
["ID"]=>
string(21) "xxxxx"
["PropertyID"]=>
string(21) "xxxxx"
["MainPhoto"]=>
bool(false)
["Name"]=>
string(2) "xxxxx"
["Type"]=>
string(5) "Photo"
["PhotoUrl"]=>
string(94) "xxxxxxx"
}
}
}
}
我的工作方法,目前的樣子
例子,但我想,以防止其重新使用此爲3個不同的值:
$array = (array) $letmc->Facilities;
$comments = array();
if (!empty($array)) {
foreach ($letmc->Facilities->FacilityInfo as $Facility) {
$comments[] = $Facility->Comment;
}
}
unset($array);
$property_comments = get_post_meta($post_id, 'property_comments', true);
$result = array_diff($comments, $property_comments);
if (!empty($result)) {
$this->update_post_meta($post_id, 'property_comments', $comments);
}
$array = (array) $letmc->Photos;
$photos = array();
if (!empty($array)) {
foreach ($letmc->Photos->PhotoInfo as $Photo) {
$photos[] = $Photo->PhotoUrl;
}
}
unset($array);
$property_photos = get_post_meta($post_id, 'property_photos', true);
$result = array_diff($photos, $property_photos);
if (!empty($result)) {
$this->update_post_meta($post_id, 'property_photos', $photos);
}
對象的'Target'酒店有'public'接入,讓你從它的類定義之外訪問它。一個更好的選擇是創建一個訪問器公共方法來獲得該值,所以,你會有這樣的東西:'$ value-> getTarget()'; – NaijaProgrammer
你可以使用'__get()'魔術方法來檢索變量類成員:http://stackoverflow.com/questions/4713680/php-get-and-set-magic-methods – CD001
@NaijaProgrammer你們中的任何一個都可以實現你的答案,我會標記正確...我不完全知道如何解決方案 –