How can I get rid of this 'PHP Notice: Undefined offset: 0 in' and reduce my lines of code?

I have the following code..

Which produces this error

PHP Notice: Undefined offset: 0 in ....

I'm checking various items in xml, its this line that produces the error.
$RDpurchaseid = $result[0];

As you can see my code is very simplistic, what can I do to get rid of the error and have less lines of code ?
$conON = "OrderNotification";
$conRD = "RefundDone";
$conFRD = "FraudRefundDone";
$conCBLN = "ChargebackLetter";
$conCBN = "Chargeback";
$conCBR = "ChargebackReversal";
$conPurchaseID = "/Purchase/PurchaseId";
$conDeliveryEmail = "/Purchase/CustomerData/DeliveryContact/Email";
$conBillingEmail = "/Purchase/CustomerData/BillingContact/Email";
$conPayStatus = "/Purchase/PaymentStatus";
$conPurchaseDate = "/Purchase/PurchaseDate";
include "shareitxml.php";
$xmlstr = utf8_encode($xmlstr);
#$xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOERROR | LIBXML_NOWARNING);
$xml = simplexml_load_string($xmlstr);
$result = $xml->xpath($conON . $conPurchaseID);
$ONpurchaseid = $result[0]; }
$result = $xml->xpath($conRD . $conPurchaseID);
$RDpurchaseid = $result[0];
$result = $xml->xpath($conFRD . $conPurchaseID);
$FRDpurchaseid = $result[0];
$result = $xml->xpath($conCBLN . $conPurchaseID);
$CBLNpurchaseid = $result[0];
$result = $xml->xpath($conCBN . $conPurchaseID);
$CBNpurchaseid = $result[0];
$result = $xml->xpath($conCBR . $conPurchaseID);
$CBRpurchaseid = $result[0];
$NotifyType = "";
if ($ONpurchaseid != "") {
    $NotifyType = $conON;
} elseif ($RDpurchaseid != "") {
    $NotifyType = $conRD;
} elseif ($FRDpurchaseid != "") {
    $NotifyType = $conFRD;
} elseif ($CBLNpurchaseid != "") {
    $NotifyType = $conCBLN;
} elseif ($CBNpurchaseid != "") {
    $NotifyType = $conCBN;
} elseif ($CBRpurchaseid != "") {
    $NotifyType = $conCBR;
$result = $xml->xpath($NotifyType . $conDeliveryEmail);
$deliveremail = $result[0];
$result = $xml->xpath($NotifyType . $conBillingEmail);
$billingemail = $result[0];
$result = $xml->xpath($NotifyType . $conPayStatus);
$paymentstatus = $result[0];
$result = $xml->xpath($NotifyType . $conPurchaseID);
$purchaseid = $result[0];
$result = $xml->xpath($NotifyType . $conPurchaseDate);
$purchasedate = $result[0];
echo "DE=" . $deliveremail . "<BR/>";
echo "BE=" . $billingemail . "<BR/>";
echo "PS=" . $paymentstatus . "<BR/>";
echo "PI=" . $purchaseid . "<BR/>";
echo "PD=" . $purchasedate . "<BR/>";

Open in new window

Who is Participating?
twocandlesConnect With a Mentor Commented:
Then check it using:

isset( $result[0] )

after retrieving the value. Thus you'll know whether the data exists.
You have an extra curly bracket "}" at the end of line 21. Try removing it...
mindwarpltdAuthor Commented:
Yes, sorry, thats a mistake while I've copied it across, but the error still remains etc
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

mindwarpltdAuthor Commented:
For completeness here is shareitxml.php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<e5Notification xmlns:xsi=""
<Street1>Vogelsanger Str. 78</Street1>
<Street1>Vogelsanger Str. 78</Street1>
<ProductName>QA Product XML mails</ProductName>

Open in new window

The notice means an access to an array element that doesn't exist. In fact, in your xml i can't see the RefundDone/Purchase/PurchaseID element you're trying to access in the offending line. Try adding it to the xml.
mindwarpltdAuthor Commented:
Yes your correct.
However, thats the point, I'm trying to detect which xml data has been used it could be



A lot of my code is used to determine which xml data is being used and getting the right data.
mindwarpltdAuthor Commented:
Great, that does get rid of the error.

Any ideas about reducing the lines of code I'm using, by some clever code to detect which data is being used etc?
When in this situations where you have to "if" for a lot of values, I usually go for this approach: put the values you want to check in an array, and loop through the array. Look at the code I'm attaching (I haven't checked if it works, but I expect you get the idea)

$valuesToCheck = array( "OrderNotification", "RefundDone", "FraudRefundDone" );
foreach( $valuesToCheck as $value) 
   $valueToUse = $xml->xpath($value . $conPurchaseID);
   if( isset( $valueToUse ) )
// Here you'll have the set value

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.