Q:

PHP utility function for SOAP answer

I have a SOAP response that looks like this
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetShowInformationResponse xmlns="http://www.xxxx.com/xxxxWS/">
      <GetShowInformationResult>
        <CustomFields>
          <CustomField>
            <Name>string</Name>
            <Value>string</Value>
          </CustomField>
          <CustomField>
            <Name>string</Name>
            <Value>string</Value>
          </CustomField>
        </CustomFields>
      </GetShowInformationResult>
    </GetShowInformationResponse>
  </soap:Body>
</soap:Envelope>

My SOAP call gives me a list of custom fields:
$result->GetShowInformationResult->CustomFields->CustomField

It answers like this (though might be object or null):
Array ( [0] => stdClass Object ( [Name] => YouTubeID [Value] => XPCHmIOml0f ) [1] => stdClass Object ( [Name] => Episode Title [Value] => Raw Foods ) )

I need to write a utility function that extracts the field I want based on the 'name' and then gives me the 'value'. Here's what I have so far, but can't get it to work...
function ExtractCustomField($fieldName, $customFields) {
 // $customFields might be an object, null, or an array.
  $parsed = array();
  if (is_array($customFields) == false && $customFields != null) {
   $parsed = array($customFields);
  } elseif (is_array($customFields)) {
   $parsed = $customFields;
  }

  // loop through the fields and find the one we are looking for
  $returnValue = null;
  foreach($field as $customFields) {
   if ($field->Name == $fieldName) {
    $returnValue = $field->Value;
    break;
   }
  }

  return $returnValue;
}

ExtractCustomField('YouTubeID','$result->GetShowInformationResult->CustomFields->CustomField');

A:

I'm dumb and @hakre helped me solve it.
Problem was mixing up variables in the foreach, and not having the function echo. Also discovered if it returned an object I wasn't putting it into an array correctly.
function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, null, or an array.
 if($customFields == null) {
   $customFields = array();       
 } else {
   $customFields = is_array($customFields) ? $customFields : array($customFields);
 }

  // loop through the fields and find the one we are looking for
  $returnValue = null;
  foreach($customFields as $field) {
   if ($field->Name == $fieldName) {
    $returnValue = $field->Value;
    break;
   }
  }

  return $returnValue;
}

echo ExtractCustomField('YouTubeID',$result->GetShowInformationResult->CustomFields->CustomField);

