This may not be possible, but it would be good to know either way.
I'm looking to use a method to run a process that's repeated in a custom class I wrote.
One of the function parameters, Target, needs to refer to an object key in $values, so in the snippet below, it relates to $value->Target.
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 );
}
}
I know this code is currently broken...
Can this be done?
EDIT
Referring to $value->Target, where $value is the object example below, I'm trying to access 'Comment', so $value->comment:
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"
}
}
}
}
Example of how my working method currently looks, but I'd like to prevent having to reuse this for 3 different values:
$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 );
}