0

I am getting "Undefined Index FruitOrder" in an if statement for an array which is created from XML/Soap Response

Code:

$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $resp);
    $xml = new SimpleXMLElement($response);
    $body = $xml->xpath('//soapBody ')[0];
    $array = json_decode(json_encode($body), TRUE);

    if($array['FruitOrder']['FruitCode'] == 'FruitSuccess' || $array['FruitOrder']['FruitCode'] == 'FruitSuccessWarnings')
    {
JustCode
  • 121
  • 9

1 Answers1

0

To avoid the Undefined Index error you should check if the item ['FruitOrder']['FruitCode'] exists in the array with isset.

    if(isset($array['FruitOrder']['FruitCode']) 
    && ($array['FruitOrder']['FruitCode'] === 'FruitSuccess'
    || $array['FruitOrder']['FruitCode'] === 'FruitSuccessWarnings'))
    {
        echo 'process the data';
    }
pulzarraider
  • 2,297
  • 19
  • 26